Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.Exit takes long time to close the application

Tags:

c#

environment

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.

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.

like image 436
ehh Avatar asked Aug 31 '15 12:08

ehh


1 Answers

What's happening behind the scenes is quite clear if you understand how .NET applications work, and how Environment.Exit works.

In short, Environment.Exit is not the fastest way to exit an arbitrary application. It still waits for any pending finalizers to run (if they run fast enough, they only have 30 seconds total IIRC). If you're using e.g. 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.

like image 134
Luaan Avatar answered Oct 20 '22 14:10

Luaan