gpt4 book ai didi

c# - Application.Exit() 与 Application.ExitThread() 与 Environment.Exit()

转载 作者:IT王子 更新时间:2023-10-29 04:08:13 25 4
gpt4 key购买 nike

我想弄清楚我应该使用哪个。在关闭我的 WinForm 应用程序时,会在对话框模式下触发一个表单。该表单运行一个后台工作程序,该工作程序将数据库与远程数据库同步,并在“Splash Form”上显示它的进度。

我有这样一个方法:

private void CloseMyApp()
{
SaveUserSettings();

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

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

这是我从 Menu --> ExitForm_FormClosing() 事件中关闭我的应用程序时调用的。 Application.Exit() 给出以下错误 -->

Collection was modified; enumeration operation may not execute.

现在我读到 Environment.Exit() 很残酷,这意味着您的应用可能有问题(请参阅 here)。

Application.ExitThread() 有效,但我担心它可能只是表面上有效,因为我之前从未使用过它,所以我不确定通常什么时候适合这样做。

最佳答案

不幸的是,问题不是由任何这些引起的,并且在所有这些情况下都确实存在(即使您没有收到消息)。

你的问题是这样的:

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

由于您在请求关闭时实际上并未关闭,因此所有“退出”函数都试图关闭您的后台线程。不幸的是,这可能发生在您的数据库同步过程中,并且在保存逻辑中工作的枚举可能会提供该错误。

我建议不要使用任何这些 - 只需调用 myMainForm.Close() 即可。那应该关闭您的主窗体,这将适本地触发您的关闭逻辑。一旦应用程序中的主窗体关闭,它将正常关闭。

关于c# - Application.Exit() 与 Application.ExitThread() 与 Environment.Exit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1312885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com