The solution by digimanus works, but it needs explanation, and the article referenced is pretty bad.
The exit methods of
System.Windows.Forms.Application
actually have nothing to do with the termination of the process where application is running. They only cause the method
Application.Run
to return.
The method
Application.Exit
exits the application but completes message pumping, so
Application.Run
does not return immediately. The method
Application.ExitThread
returns from the loop; so it happens as soon as the current message is dispatched and execution returns to the loop.
These method affect only one thread, the UI thread. So, the process is not terminated if there is another thread which is kept executing. This is the most typical reason of execution of the process after "normal"
Application.Exit
. The method
System.Environment.Exit
works anyway. However, I would not recommend it. The threads should be correctly terminated or correctly aborted because some post-processing in the aborted thread can be important.
System.Environment.Exit
is a careless way. The best way of accurate exit is closing of the main form and doing all post-processing including thread termination triggered in its
FormClosed
event.
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx[
^].
—SA