简体   繁体   中英

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 stays 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 (ie 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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM