简体   繁体   English

Environment.Exit需要很长时间才能关闭应用程序

[英]Environment.Exit takes long time to close the application

I am trying to fix an issue that it is not clear to me. 我想解决一个我不清楚的问题。 When calling to Environment.Exit(0) , it takes about 22 sec to close the application. 调用Environment.Exit(0) ,关闭应用程序大约需要22秒。

if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
    Environment.Exit(exitCode);
}

Have you got any idea what is happening behind the scene? 你知道幕后发生了什么吗? I though maybe an exception is thrown but I checked all the checkboxes in Exceptions dialog. 我可能会抛出异常,但我检查了“例外”对话框中的所有复选框。

What's happening behind the scenes is quite clear if you understand how .NET applications work, and how Environment.Exit works. 如果您了解.NET应用程序的工作方式以及Environment.Exit工作原理,那么幕后发生的事情就非常清楚了。

In short, Environment.Exit is not the fastest way to exit an arbitrary application. 简而言之, Environment.Exit不是退出任意应用程序的最快方法。 It still waits for any pending finalizers to run (if they run fast enough, they only have 30 seconds total IIRC). 它仍然等待任何待定的终结器运行(如果它们运行得足够快,它们总共只有30秒的IIRC)。 If you're using eg sockets, it's perfectly possible that the finalizers take a few seconds to run. 如果您使用的是插座,那么终结器完全可能需要几秒钟才能运行。

The easiest way to check is of course to use the debugger - just Pause when doing the shutdown, and you'll see which threads are actually executing, and what it is they are executing. 最简单的检查方法当然是使用调试器 - 只是在执行关闭时暂停,您将看到哪些线程实际正在执行,以及它们正在执行什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM