The question in the title does not refer to the actions you take when you are about to exit your application, issues like logging, closing file handles, freeing unmanaged resources and so on. It does literally refer to the exit action itself:

  • Do you use Application.Exit or Environment.Exit?
  • Where do you call the exit method?

The idea for that post came to my mind after I had to solve a weird bug where pressing the Exit button led to a freeze in an application that I work on. It turned out that I was not using the proper Exit method although I was aware to the fact that there are 2 possibilities. Sometimes, when you are in a middle of a coding momentum, writing a lot of code, you can miss the little details. Those will come back to hunt you later on,  and the most difficult thing about it is that everything was functioning fine for about a year before the bug showed his ugly face.

Lets first understand the difference between the 2 Exit methods:

  1. Environment.Exit – From MSDN: Terminates this process and gives the underlying operating system the specified exit code. This is the code to call when you are using console application.
  2. Application.Exit – From MSDN: Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This is the code to use if you are have called Application.Run (WinForms applications), this method stops all running message loops on all threads and closes all windows of the application. There are some more issues about this method, read about it in the MSDN page.

So, my lesson was that I should not call Environment.Exit if I am writing an application which have a message loop (can be checked using the Application.MessageLoop property)! Application.Exit shall have been used…

Another important note to take, is that those calls shall not be made in any component assembly but in the assembly which contains the main method. This is important because component assemblies might be used by both console applications and WinForms applications and using one of the Exit methods will be wrong…

Tags :

4 Responses to “How Do You Exit Your .Net Application?”

  1. Al Gonzalez

    Said on June 24, 2008 :

    For WinForms applications, I just close the main window. The main window is responsible for closing any child windows. So in your case, when the exit button is pressed I’d call this.Close(); (or Me.Close() in VB) – assuming the exit button is on the main window.

  2. Shahar Y

    Said on June 24, 2008 :

    Hi Al,

    1) If you supperate the logic from the UI (like in MVC pattern) and you need to do other logical operation before closing – you can call Application.Exit from your Control classes.
    2) It closes all of the windows in all threads, so if you have multiple forms and/or multiple threads – you can use Application.Exit

  3. Ramthdos

    Said on July 24, 2009 :

    I think, we can use either Application.Exit or Environment.Exit in a WinForm application, basically depending on the state of your application when the method is being called.

    If you want to exit your application before calling Application.Run method, Application.Exit wouldn’t work because the message loop has not yet been created.

    Use Application.Exit only after calling Application.Run, or if you have a model dialog visible to the user. Otherwise, you should use Environment.Exit in a WinForm application.

  4. periyasamy

    Said on December 9, 2010 :

    i want to close my window in….. send clear coding me

Post a Comment