C# Application.Exit() 与 Application.ExitThread() 与 Environment.Exit()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1312885/
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
Application.Exit() vs Application.ExitThread() vs Environment.Exit()
提问by Refracted Paladin
I am trying to figure out which I should be using. On closing my WinForm app fires of a Form in Dialog mode. That form runs a Background worker that Syncs the DB with the remote DB and displays it's progress on the "Splash Form."
我想弄清楚我应该使用哪个。在关闭我的 WinForm 应用程序时,会在对话框模式下触发一个表单。该表单运行一个后台工作程序,将数据库与远程数据库同步并在“Splash 表单”上显示其进度。
I have a method like so:
我有一个这样的方法:
private void CloseMyApp()
{
SaveUserSettings();
splashForm = new SplashForm();
splashForm.ShowDialog();
Application.ExitThread();
//Application.Exit();
}
which is what I call to close my app from Menu --> Exitand in the Form_FormClosing()
event. Application.Exit()
gives the following error -->
这就是我从“菜单”->“退出”和Form_FormClosing()
事件中关闭我的应用程序所调用的方法。 Application.Exit()
给出以下错误-->
Collection was modified; enumeration operation may not execute.
集合被修改;枚举操作可能无法执行。
Now I read that Environment.Exit()
is brutal and means there is probably something wrong with your app (see here).
现在我读到这Environment.Exit()
是残酷的,这意味着您的应用程序可能有问题(请参阅此处)。
Application.ExitThread()
works but I am concered that it may only be APPEARING to work and as I have never used it before I am not sure when it is normally appropriate to do so.
Application.ExitThread()
工作,但我担心它可能只是出现工作,因为我从未使用过它,我不确定何时通常适合这样做。
采纳答案by Reed Copsey
Unfortunately, the problem isn't caused by any of these, and really exists (even if you don't get the message) in all of these scenarios.
不幸的是,问题不是由任何这些引起的,并且在所有这些场景中确实存在(即使您没有收到消息)。
Your problem is this:
你的问题是这样的:
On closing my WinForm App fires of a Form in Dialog mode. That form runs a Background worker that Syncs the DB with the remote DB and displays it's progress on the "Splash Form."
在关闭我的 WinForm 应用程序时,会在对话框模式下触发一个表单。该表单运行一个后台工作程序,将数据库与远程数据库同步并在“Splash 表单”上显示其进度。
Since you're not actually shutting down when you request a shutdown, all of the "Exit" functions are trying to tear down your background thread. Unfortunately, this is probably happening in the middle of your DB sync, and an enumeration working in the save logic is probably providing that error.
由于您在请求关闭时实际上并未关闭,因此所有“退出”功能都试图拆除您的后台线程。不幸的是,这可能发生在您的数据库同步过程中,并且在保存逻辑中工作的枚举可能会提供该错误。
I would recommend not using any of these - just call myMainForm.Close()
instead. That should close your main form, which will fire your closing logic appropriately. Once the main form in your application closes, it will shut down gracefully.
我建议不要使用任何这些 - 只需调用即可myMainForm.Close()
。这应该关闭您的主表单,这将适当地触发您的关闭逻辑。一旦您的应用程序中的主窗体关闭,它将正常关闭。
回答by Alan
Environment.Exit()
is used for console apps.
Environment.Exit()
用于控制台应用程序。
You want to use: System.Windows.Forms.Application.Exit()
你想使用: System.Windows.Forms.Application.Exit()
By exiting thread, you are only exiting the current thread context, while leaving any started foreground threads running. I suspect the thread that is causing the error is still running, so you've essentially masked the problem, not worked around it. I would try and figure out why you are getting this error "Collection was modified; enumeration operation may not execute."
on exit. It's being exposed by Application.Exit()
, but it's not caused by it.
通过退出线程,您只是退出当前线程上下文,而让任何已启动的前台线程继续运行。我怀疑导致错误的线程仍在运行,因此您基本上掩盖了问题,而不是解决它。我会尝试弄清楚为什么您"Collection was modified; enumeration operation may not execute."
在退出时收到此错误。它是由 暴露的Application.Exit()
,但不是由它引起的。