C# 为什么 Application.Exit 无法工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/554408/
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 would Application.Exit fail to work?
提问by Tony Peterson
I have an application that has been getting strange errors when canceling out of a dialog box. The application can't continue if the box is cancelled out of, so it exits, but it is not working for some reason, and thus it keeps running and crashes.
我有一个应用程序在取消对话框时出现奇怪的错误。如果框被取消,应用程序将无法继续,因此它退出,但由于某种原因无法正常工作,因此它继续运行并崩溃。
I debugged this problem, and somehow the application runs right past the Application.Exit call. I'm running in Debug mode, and this is relevant because of a small amount of code that depends on the RELEASE variable being defined. Here is my app exit code. I have traced the code and it entered the ExitApp method, and keeps on going, returning control to the caller and eventually crashing.
我调试了这个问题,并且以某种方式应用程序在 Application.Exit 调用之后运行。我在调试模式下运行,这很重要,因为有少量代码依赖于定义的 RELEASE 变量。这是我的应用程序退出代码。我跟踪了代码,它进入了 ExitApp 方法,并继续前进,将控制权返回给调用者并最终崩溃。
This is an application which provides reports over a remote desktop connection, so that's why the exit code is a bit weird. Its trying to terminate the remote session, but only when running under release because I don't want to shut down my dev machine for every test run.
这是一个通过远程桌面连接提供报告的应用程序,这就是退出代码有点奇怪的原因。它试图终止远程会话,但仅在发布时运行,因为我不想在每次测试运行时关闭我的开发机器。
private void ExitApp()
{
HardTerminalExit();
Application.Exit();
}
// When in Debug mode running on a development computer, this will not run to avoid shutting down the dev computer
// When in release mode the Remote Connection or other computer this is run on will be shut down.
[Conditional("RELEASE")]
private void HardTerminalExit()
{
WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false);
}
I've run a debugger right past the Application.Exit line and nothing happens, then control returns to the caller after I step past that line.
我在 Application.Exit 行后运行了一个调试器,但没有任何反应,然后在我越过该行后控制权返回给调用者。
What's going on? This is a Windows Forms application.
这是怎么回事?这是一个 Windows 窗体应用程序。
采纳答案by GEOCHET
This is an article which expands on the same train of thought you are going through: http://www.dev102.com/2008/06/24/how-do-you-exit-your-net-application/
这篇文章扩展了您正在经历的相同思路:http: //www.dev102.com/2008/06/24/how-do-you-exit-your-net-application/
Basically:
基本上:
Environment.Exit - From MSDN: Terminates this process and gives the underlying operating system the specified exit code. This is the code to call when you are using console application.
Application.Exit - From MSDN: Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This is the code to use if you are have called Application.Run (WinForms applications), this method stops all running message loops on all threads and closes all windows of the application. There are some more issues about this method, read about it in the MSDN page.
Environment.Exit - 来自 MSDN:终止此进程并为底层操作系统提供指定的退出代码。这是使用控制台应用程序时要调用的代码。
Application.Exit - 来自 MSDN:通知所有消息泵它们必须终止,然后在处理消息后关闭所有应用程序窗口。如果您已调用 Application.Run(WinForms 应用程序),则这是要使用的代码,此方法会停止所有线程上的所有正在运行的消息循环并关闭应用程序的所有窗口。关于此方法还有一些问题,请在 MSDN 页面中阅读。
Another discussion of this: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
对此的另一个讨论:http: //geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
This article points out a good tip:
这篇文章指出了一个很好的提示:
You can determine if System.Windows.Forms.Application.Run has been called by checking the System.Windows.Forms.Application.MessageLoop property. If true, then Run has been called and you can assume that a WinForms application is executing as follows.
您可以通过检查 System.Windows.Forms.Application.MessageLoop 属性来确定 System.Windows.Forms.Application.Run 是否已被调用。如果为 true,则 Run 已被调用,您可以假设 WinForms 应用程序按如下方式执行。
if (System.Windows.Forms.Application.MessageLoop)
{
// Use this since we are a WinForms app
System.Windows.Forms.Application.Exit();
}
else
{
// Use this since we are a console app
System.Environment.Exit(1);
}
回答by configurator
Is this application run (in the Main
method) using Application.Run()
? Otherwise, Application.Exit()
won't work.
此应用程序是否运行(在Main
方法中)使用Application.Run()
?否则,Application.Exit()
将无法工作。
If you wrote your own Main
method and you want to stop the application, you can only stop by returning from the Main
method (or killing the process).
如果您编写了自己的Main
方法并想停止应用程序,则只能通过从Main
方法返回(或杀死进程)来停止。
回答by Nick Gunn
Try Environment.Exit(exitCode)
.
试试Environment.Exit(exitCode)
。
回答by Ahmet Murati
I have went though this situation in many cases I use Thread.CurrentThread.Abort()
我在很多情况下都经历过这种情况,我使用 Thread.CurrentThread.Abort()
and the process is closed. It seems that Application.Exit isn't hooking up properly with current thread.
并且该过程已关闭。似乎 Application.Exit 没有与当前线程正确连接。
回答by Andrew
Also ensure any threads running in your application have the IsBackground property set to true. Non-background threads will easily block the application from exiting.
还要确保应用程序中运行的任何线程都将 IsBackground 属性设置为 true。非后台线程很容易阻止应用程序退出。
回答by Mark Bad
Make sure you have no Console.ReadLine(); in your app and Environment.Exit(1); will work and close your app.
确保你没有 Console.ReadLine(); 在您的应用程序和 Environment.Exit(1) 中;将工作并关闭您的应用程序。
回答by StigM
Having had this problem recently (that Application.Exit was failing to correctly terminate message pumps for win-forms with Application.Run(new Form())), I discovered that if you are spawning new threads or starting background workers within the constructor, this will prevent Application.Exit from running.
最近遇到了这个问题(Application.Exit 无法使用 Application.Run(new Form()) 正确终止 win-forms 的消息泵),我发现如果您在构造函数中生成新线程或启动后台工作程序,这将阻止 Application.Exit 运行。
Move all 'RunWorkerAsync' calls from the constructor to a form Load method:
将所有“RunWorkerAsync”调用从构造函数移动到表单 Load 方法:
public Form()
{
this.Worker.RunWorkerAsync();
}
Move to:
搬去:
public void Form_Load(object sender, EventArgs e)
{
this.Worker.RunWorkerAsync();
}
回答by Ali Gh
Try this :
尝试这个 :
in Program.cs file :
在 Program.cs 文件中:
after Application.Run(new form());
在 Application.Run(new form()) 之后;
add Application.Exit();
添加Application.Exit();