C# Environment.Exit() 和 Application.Shutdown() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/905544/
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
what's difference between Environment.Exit() and Application.Shutdown()?
提问by Cooper.Wu
Sometimes application can't exit when I called Application.Shutdown, the UI was closed, but the process is still running. how to shutdown application with close all threads? does the Environment.Exit()could close all thread? or we should call Win32 API TerminateThreadto do it?
有时当我调用Application.Shutdown时应用程序无法退出,UI 已关闭,但进程仍在运行。如何关闭应用程序并关闭所有线程?请问Environment.Exit()可以关闭所有的线程?或者我们应该调用 Win32 API TerminateThread来做到这一点?
采纳答案by EKS
Environment.Exit()is a more brutal way of closing down your application, yes. But in general, if you need to kill your application to make it close then I think you're looking at the problem in the wrong way. You should rather look into why the other threads aren't closing gracefully.
Environment.Exit()是一种更残酷的关闭应用程序的方式,是的。但总的来说,如果您需要终止应用程序以使其关闭,那么我认为您以错误的方式看待问题。您应该研究为什么其他线程没有正常关闭。
You could look into the FormClosing eventon the main form and close down any resources that are hanging up the application, preventing it from closing.
您可以查看主窗体上的FormClosing 事件并关闭挂起应用程序的所有资源,以防止其关闭。
This is how I have found resources hanging up the app.
这就是我找到挂在应用程序上的资源的方式。
- In debug mode, enable showing of threads. (This will allow you to see all the threads your application is running.)
- Close the application in the way that it does not close correctly.
- Press pause in Visual studio.
- Look at the threads list, and click on them to see where is the code they are hanging. Now that you can see what resources are blocking your application from closing, go to your FormClosing event and close/dispose them there.
- Repeat until the app closes correctly :)
- 在调试模式下,启用显示线程。(这将允许您查看应用程序正在运行的所有线程。)
- 以无法正确关闭的方式关闭应用程序。
- 在 Visual Studio 中按暂停。
- 查看线程列表,然后单击它们以查看它们挂在何处的代码。现在您可以看到哪些资源阻止您的应用程序关闭,请转到您的 FormClosing 事件并在那里关闭/处理它们。
- 重复直到应用程序正确关闭:)
Be aware that the threads list in debug mode will show some threads that are run but not under your control. These threads rarely have a name and when you click on them you get a message saying you have no symbols. These can be safely ignored.
请注意,调试模式下的线程列表将显示一些正在运行但不受您控制的线程。这些线程很少有名称,当您单击它们时,您会收到一条消息,说您没有符号。这些可以安全地忽略。
One of the reasons for making sure your application is closing gracefully is that some resources (let's say a FileStream) are not done working, so using some API to force it to quit can make all sorts of "random" problems come in, like settings/data files not being written and so on.
确保您的应用程序正常关闭的原因之一是某些资源(比如FileStream)未完成工作,因此使用某些 API 强制其退出可能会导致各种“随机”问题出现,例如设置/data 文件没有被写入等等。
回答by Shay Erlichmen
- You should NEVERcall TerminateThread
- Make sure that all the threads that you spawn are mark as background, this way when you close the application it won't wait for them to complete.
- 你永远不应该调用TerminateThread
- 确保您生成的所有线程都标记为背景,这样当您关闭应用程序时,它不会等待它们完成。
回答by Nir
As Shay said, NEVER call TerminateThread, TerminateThread kills just one thread without letting it clean up after itself this can lead to deadlocks and corruptions in other threads in the process.
正如 Shay 所说,永远不要调用 TerminateThread,TerminateThread 只杀死一个线程而不让它自己清理,这可能导致进程中其他线程的死锁和损坏。
TerminateProcess on the other had will kill the entire process and let the OS clean up, it's the fastest way to close a process - you just have to make sure you are not holding any resources the OS can't clean up (it also helps to close windows before calling TerminateProcess).
另一方面,TerminateProcess 将终止整个进程并让操作系统清理,这是关闭进程的最快方法 - 您只需要确保您没有持有操作系统无法清理的任何资源(这也有助于在调用 TerminateProcess 之前关闭窗口)。
I think, but I haven't checked, that Environemnt.Exit calls TerminateProcess.
我想,但我还没有检查,Environemnt.Exit 调用 TerminateProcess。
Application.Shutdown is very different, it doesn't immediately kill the process - it sends all the closing and shutdown notifications and waits for all the application's windows and threads close themselves.
Application.Shutdown 非常不同,它不会立即终止进程 - 它发送所有关闭和关闭通知并等待所有应用程序的窗口和线程自行关闭。