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#?
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. TheExit()method is typically called from within a message loop, and forcesRun()to return. To exit a message loop for the current thread only, callExitThread(). This is the call to use if you are running a Windows Forms application. As a general guideline, use this call if you have calledSystem.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 haveSecurityPermissionFlag.UnmanagedCodepermissions. If you do not, aSecurityExceptionerror occurs. This is the call to use if you are running a console application.I hope it is best to use
Application.ExitSee also these links: