72

What is best practice when closing a C# application?

I have read that you can use:

Environment.Exit(0); or Application.Exit();

But what is the difference?

Furthermore, with regards to Environment.Exit(0), I have used exit codes before when working with Java but have never fully understood their purpose. What role do they play when exiting an application in C#?

tshepang
  • 12,111
  • 21
  • 91
  • 136
bobble14988
  • 1,749
  • 5
  • 26
  • 38
  • 2
    Depends on your application. If you write a demon, then return 0; if you use Windows Application, then close main form. Don't use forced termination. – cnd Aug 22 '11 at 10:30
  • 1
    Possible duplicates http://stackoverflow.com/questions/1312885/application-exit-vs-application-exitthread-vs-environment-exit – Jalal Said Aug 22 '11 at 10:31
  • 1
    @jalal may be your right some.. But, I won't say it is duplicate.. I already added that link in my post below before a min of your comment. Please don't mark as duplicate, it hope it worth. – Naga Harish M Aug 22 '11 at 10:38
  • @Windows we generally mark the question as duplicated if there is a duplicated version, I was already vote to close it and this is not undoable action, However mark the question as duplicated will not harm your answer, the user still can accept it. – Jalal Said Aug 22 '11 at 10:45
  • thx @jalal, but I am not look for accept of my ans. I also got same thing(duplicate) sometime before. I fell so bad about it because even I can't see that question in google or in related Qs. Then, how I will know it is already asked... Anyway pls don't do that again a small request bro from my side. – Naga Harish M Aug 22 '11 at 10:51
  • @Windows The duplicated post I link to is the first or second answer by google search `Environment.Exit(0); or Application.Exit();`... – Jalal Said Aug 22 '11 at 10:54
  • @Маша What's wrong with forced termination? – Kyle Delaney Mar 27 '18 at 17:41

5 Answers5

72

System.Windows.Forms.Application.Exit() - Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This method stops all running message loops on all threads and closes all windows of the application. This method does not force the application to exit. The Exit() method is typically called from within a message loop, and forces Run() to return. To exit a message loop for the current thread only, call ExitThread(). This is the call to use if you are running a Windows Forms application. As a general guideline, use this call if you have called System.Windows.Forms.Application.Run().

System.Environment.Exit(exitCode) - Terminates this process and gives the underlying operating system the specified exit code. This call requires that you have SecurityPermissionFlag.UnmanagedCode permissions. If you do not, a SecurityException error occurs. This is the call to use if you are running a console application.

I hope it is best to use Application.Exit

See also these links:

a stone arachnid
  • 1,272
  • 1
  • 15
  • 27
Naga Harish M
  • 2,779
  • 2
  • 31
  • 45
  • 5
    Might be worth pointing out explicitly that before you call `Application.Run` your only choice is `Environment.Exit` as `Application.Exit` will have no effect. – Carsten May 29 '13 at 01:41
  • 1
    I have two forms login and main form. when the login button clicked on the login form i use this.hide() when login is clicked and mainform opens and from there i use hide when form is closed then login form shows up again. if i run the application and click on login button and then close mainform many times repeatedly and when i click on exit (System.Windows.Forms.Application.Exit() )button on the login form, it will show all the login forms that i hided. but if i use (System.Environment.Exit(0)) it will close and exit the application completely . all in all i would say the later one is best – r.hamd Feb 16 '16 at 09:02
18

Application.Exit is for Windows Forms applications - it informs all message pumps that they should terminate, waits for them to finish processing events and then terminates the application. Note that it doesn't necessarily force the application to exit.

Environment.Exit is applicable for all Windows applications, however it is mainly intended for use in console applications. It immediately terminates the process with the given exit code.

In general you should use Application.Exit in Windows Forms applications and Environment.Exit in console applications, (although I prefer to let the Main method / entry point run to completion rather than call Environment.Exit in console applications).

For more detail see the MSDN documentation.

Justin
  • 84,773
  • 49
  • 224
  • 367
  • 1
    Thanks for the answer. I am needing to exit my application from a different class rather than Program.cs and it is a console application, and this works great. I could use the keyword return as well I believe and that would let the Main finish running, but it makes no difference in this case. – Radmation Apr 20 '16 at 19:54
0

Try this :

System.Windows.Application.Current.Shutdown();

It is work also with NotifyIcon. Place this code in App.xaml.cs :

protected override void OnExit(ExitEventArgs e)
{
    base.OnExit(e);
    App.nIcon.Visible = false;
}
j.developer
  • 474
  • 2
  • 5
  • 15
0

What role do they play when exiting an application in C#?

The same as every other application. Basically they get returned to the caller. Irrelvant if ythe start was an iicon double click. Relevant is the call is a batch file that decides whether the app worked on the return code. SO, unless you write a program that needs this, the return dcode IS irrelevant.

But what is the difference?

One comes from environment one from the System.Windows.Forms?.Application. Functionall there should not bbe a lot of difference.

TomTom
  • 61,059
  • 10
  • 88
  • 148
-1

for me best solotion this is

 Thread.CurrentThread.Abort();

and force close app.

porya ras
  • 458
  • 5
  • 15