为什么 Application.Exit() 不会退出 Windows 窗体应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2151685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't Application.Exit() quit a Windows Forms application?
提问by Casey
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.
我正在使用 C# 学习 Windows 窗体,并且有一个基本的应用程序。当用户选择 File->Exit 时,我试图关闭它。我有一个附加到它的事件处理程序,我尝试调用Application.Exit()
,Application.ExitThread()
然后关闭表单。没有。它留在那里。我也没有创建任何其他线程。
Ideas? Thanks.
想法?谢谢。
回答by codekaizen
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.
如果是这样,如果没有传递窗口消息(即 UI 线程被阻塞),应用程序将不会退出。测试这一点的一种方法是调用Environment.Exit()
哪个更残酷地强制关闭。如果这成功了,您就可以找出Application.Exit()
不工作的原因。
回答by John Knoeller
Application.Exit
isn't the normal way to close a GUI application. Use form.Close
instead.
Application.Exit
不是关闭 GUI 应用程序的正常方式。使用form.Close
来代替。
private static void OnMenuClose_Click(object sender, System.EventArgs e)
{
Form dlg = ((Control) sender).FindForm();
//dlg.DialogResult = DialogResult.OK;
dlg.Close();
}