Use the Application.Exit[^] method to terminate the application. If specific clean up actions are required, include an event handler like:
void Application_Exit ( object sender,
EventArgs e )
{
}
and in the application's constructor add the statement:
Application.ApplicationExit +=
new EventHandler (
Application_Exit );
Alternatively, if you have a handle to the MessageBox window, you could send the MessageBox window a WM_CLOSE message. (You can obtain the handle to the MessageBox window using FindWindow.)
Updated 2-Apr-14 7:04am
v2
For close active message, i refer these
MessageBox with Okhttp://stackoverflow.com/questions/12532812/how-to-suppress-a-dialog-box-displayed-by-code-that-i-cant-change
MessabeBoxWith Yes or No
http://stackoverflow.com/questions/16797370/click-a-messagebox-button-programmatically
#region MessageBox Closing Method
//// === Close MessageBox Programcaticaly, other than MessageBoxButton.yesNo type ===============
//// === Programaticaly click on MessageBox No button
//// ============================================================================================
private bool cancelled;
// P/Invoke declarations
private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
////public class DialogCloser : IDisposable
////{
public void CloseActiveMessageBoxOnApplicationIdle()
{
//if (Application.OpenForms.Count == 0) throw new InvalidOperationException();
//Application.OpenForms[0].BeginInvoke(new Action(() =>
//{
// Enumerate windows to find dialogs
if (cancelled) return;
EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero);
GC.KeepAlive(callback);
//}));
}
public void Dispose()
{
cancelled = true;
}
private static bool checkWindow(IntPtr hWnd, IntPtr lp)
{
// Checks if <hwnd> is a Windows dialog
StringBuilder sb = new StringBuilder(260);
GetClassName(hWnd, sb, sb.Capacity);
if (sb.ToString() == "#32770")
{
// Programaticaly click on 'X' button on MessageBox
// Close it by sending WM_CLOSE to the window (MessageBox.OK, OkCancel, type)
SendMessage(hWnd, 0x0010, IntPtr.Zero, IntPtr.Zero);
// Programaticaly click on 'No' button on MessageBox
// Close MessageBox.YesNo type
EnumChildWindows(hWnd, EnumChildWindowsCallback, IntPtr.Zero);
}
return true;
}
//// === End Close MessageBox Programcaticaly, other than MessageBoxButton.yesNo type ===========
//// ============================================================================================
//// =========== Close MessageBox.YesNo type ====================================================
//// =========== Programcaticaly click on MessageBox No button. =================================
//// ============================================================================================
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
//[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
//private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
private static extern IntPtr GetWindowCaption(IntPtr hwnd, StringBuilder lpString, int maxCount);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
// A delegate which is used by EnumChildWindows to execute a callback method.
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
//// This method accepts a string which represents the title name of the window you're looking for the controls on.
//public static void ClickButtonLabeledNo(string windowTitle)
//{
// try
// {
// // Find the main window's handle by the title.
// var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);
// // Loop though the child windows, and execute the EnumChildWindowsCallback method
// EnumChildWindows(windowHWnd, EnumChildWindowsCallback, IntPtr.Zero);
// }
// catch (Exception e)
// {
// MessageBox.Show(e.ToString());
// }
//}
private static bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
{
const uint WM_LBUTTONDOWN = 0x0201;
const uint WM_LBUTTONUP = 0x0202;
var sb = new StringBuilder(256);
// Get the control's text.
GetWindowCaption(handle, sb, 256);
var text = sb.ToString();
// If the text on the control == &No send a left mouse click to the handle.
if (text == @"&No")
{
PostMessage(handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(handle, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}
return true;
}
//// =========== End Programcaticaly click on MessageBox No button. =============================
//// ============================================================================================
#endregion MessageBox Closing Method
and i will call the method CloseActiveMessageBoxOnApplicationIdle(), and its working fine. and i would like to know, is this a good method.