Why won't Application.Exit() quit a Windows Forms application?
I am working on learning Windows Forms with C# and have a bare bones application. I am trying to close it when the user selects File->Exit. I have an event handler attached to it and I have tried calling Application.Exit()
, Application.ExitThread()
and just closing the form. Nothing. It sta开发者_开发技巧ys there. I'm not creating any other threads of any sort either.
Ideas? Thanks.
Have you tried to put a breakpoint in the event handler to see if it is being hit?
If so, the application won't exit if the window messages aren't being delivered (i.e. the UI thread is blocked). One way to test this is to call Environment.Exit()
which is more brutal about forcing a close. If this succeeds, you can then figure out why Application.Exit()
isn't working.
Application.Exit
isn't the normal way to close a GUI application. Use form.Close
instead.
private static void OnMenuClose_Click(object sender, System.EventArgs e)
{
Form dlg = ((Control) sender).FindForm();
//dlg.DialogResult = DialogResult.OK;
dlg.Close();
}
精彩评论