Accepted answer
Score: 37

Application.Exit really just asks the message loop very 6 gently.

If you want your app to exit, the 5 best way is to gracefully make it out of 4 Main, and cleanly close any additional non-background 3 threads.

If you want to be brutal... Environment.Exit or 2 Environment.FailFast? note this is harsh - about the same as killing 1 your own Process.

Score: 17

Try the following:


Environment.Exit doesn't work with Winforms 1 and Environment.FailFast throws its own exception.

Score: 4

If your application does not exit gracefully 11 when you call Application.Exit there is (obviously) something 10 that prevents it from doing so. This can 9 be anything from a form setting e.Cancel = true in the 8 FormClosing event, to a thread that is not a background 7 thread that is still running. I would advice 6 you to carefully research exactly what it 5 is that keeps your process alive, and close 4 that in a nice manner. That should make 3 your application close nicely as well.

Typically, in 2 a winforms application, it should be sufficient 1 to close the main form.

Score: 3

I use

if (System.Windows.Forms.Application.MessageLoop)
   // Use this since we are a WinForms app
   // Use this since we are a console app

from http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx


Score: 1

I had the same problem when I discovered 8 opening a new form/window within the program, and 7 only HIDING that second form (not closing 6 it), would prevent the main form from properly 5 quitting via Application.Exit();

There are 4 two solutions in this case. First is to 3 simply use Close() on the main form instead 2 of Application.Exit(). Second solution is 1 to use the following code:

if (secondForm != null && !secondForm.IsDisposed) secondForm.Dispose();

More Related questions