Application.Exit()与Application.ExitThread()与Environment.Exit() [英] Application.Exit() vs Application.ExitThread() vs Environment.Exit()

查看:879
本文介绍了Application.Exit()与Application.ExitThread()与Environment.Exit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出我应该使用。在关闭表单我的WinForm程序火灾对话模式。这种形式运行的后台工作的同步操作数据库与远程数据库,并显示它的进展情况扑通表。

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."

我有像这样的方法:

private void CloseMyApp()
{
    SaveUserSettings();

    splashForm = new SplashForm();
    splashForm.ShowDialog();

    Application.ExitThread();
    //Application.Exit();
}

这就是我所说的,从菜单关闭我的应用程序 - >退出 Form_FormClosing()事件。 Application.Exit()提供了以下错误 - >

which is what I call to close my app from Menu --> Exit and in the Form_FormClosing() event. Application.Exit() gives the following error -->

集合已修改;枚举操作可能无法执行。

Collection was modified; enumeration operation may not execute.

现在,我读了 Environment.Exit()是残酷和手段有可能出错了您的应用程序(见<一href="http://stackoverflow.com/questions/905544/whats-difference-between-environment-exit-and-application-shutdown/905584#905584">here).

Now I read that Environment.Exit() is brutal and means there is probably something wrong with your app (see here).

Application.ExitThread()的作品,但我concered,它可能只出现工作,因为我从来没有使用它之前,我不知道当它是正常适当的话。

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.

推荐答案

不幸的是,这个问题是不是由任何这些引起的,确实存在(即使你没有得到的消息)在所有这些情况。

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.

您的问题是这样的:

在关闭表单我的winform应用程序火灾对话模式。这种形式运行的后台工作的同步操作数据库与远程数据库,并显示它的进展情况扑通形式。

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."

既然你没有真正关闭,当你要求关机,所有的退出的功能正在试图推倒你的后台线程。不幸的是,这可能发生在你的数据库同步的中间,并在保存逻辑枚举工作很可能提供了错误。

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.

我会建议不使用任何这些 - 只需拨打 myMainForm.Close()代替。这应该关闭你的主要形式,这将适当地激发您的交易逻辑。一旦你的应用程序的主窗体关闭,它会正常关闭。

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.

这篇关于Application.Exit()与Application.ExitThread()与Environment.Exit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆