87

Following are the ways by which we can exit an application:

  1. Environment.Exit(0)
  2. Application.Exit()
  3. Form.Close()

What is the difference between these three methods and when to use each one?

Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
Parag Meshram
  • 8,281
  • 10
  • 52
  • 88
  • 9
    This is not a duplicate question. I am not asking which one to use to exit the application. I am just asking for the difference between these methods as it performs similar operation. – Parag Meshram Oct 13 '16 at 05:04

2 Answers2

138

The proper method would be Application.Exit(). According to the Documentation, it terminates all message loops and closes all windows thus giving your forms the possibility to execute their cleanup code (in Form.OnClose etc).

Environment.Exit would just kill the process. If some form has e.g. unsaved changes it would not have any chances to ask the user if he wants to save them. Also resources (database connections etc.) could not be released properly, files might not be flushed etc.

Form.Close just does what it says: it closes a form. If you have other forms opened (perhaps not now but in some future version of your application), the application will not terminate.

Keep in mind that if you use multithreading, Application.Exit() will not terminate your threads (and thus the application will keep working in the background, even if the GUI is terminated). Therefore you must take measures to kill your threads, either in the main function (i.e. Program.Main()) or when in the OnClose event of your main form.

MartinStettner
  • 28,719
  • 15
  • 79
  • 106
  • 4
    You can set the IsBackground property on threads so that they don't prevent the process from terminating. – Rolf Bjarne Kvinge Oct 24 '12 at 09:58
  • 1
    Is what you say about flushing files really true? When a process is killed the OS should close all existing handles which also flushes the handles. – Esben Skov Pedersen Oct 24 '12 at 10:34
  • 3
    @EsbenSkovPedersen but there might be internal caches that the OS doesn't know about (in the app, in the .Net framework, in third-party libraries, etc.) – MarkJ Oct 24 '12 at 10:48
  • @Giorgi That depends very much on how the thread(s) are designed, and it's a question on its own. Usually the best way ist to provide some mechanism to "tell" the thread to finish its work. Then you could `Thread.Join()` it, to wait until it's really finished and call `Application.Exit()` afterwards. You also *could* call `Thread.Abort()` but this is usually not a good option and should be used with caution. Search for questions like http://stackoverflow.com/questions/1051838/killing-a-net-thread , I guess there are good answers on StackOverflow... – MartinStettner May 28 '15 at 23:33
  • 1
    Your note on `Form.Close` is not true. If the main form is closed the whole app goes with it, without raising any of the `FormClosing`/`FormClosed` of child forms. The prcoess exits. `Application.Exit` on the other hand lets you gracefully handle each child form's closing/closed events. – nawfal Aug 17 '22 at 17:11
  • For a Windows Forms in 2022 I had to use `Environment.Exit( 0 )`. If I used `Application.Exit()` the app kept running. This was true even if `Application.Exit()` came before `Application.Run(new Form1())`! – Ed Graham Dec 05 '22 at 14:19
4

they are all fine. but form.Close() won't close your application it closes the form and after that the main-method returns an int (exitcode).

if you want that your application exits with exitcodes use Environmet.Exit(exitcode) or return the exitcode in the main-method

codeteq
  • 1,502
  • 7
  • 13