Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Applications

Tags:

c#

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#?

like image 796
bobble14988 Avatar asked Aug 22 '11 10:08

bobble14988


People also ask

What does it mean to close applications?

To close apps on Android means to shut the apps down. You might shut down an app if it isn't responding normally, if your phone or tablet is low on memory, or to clear up the screen. Closing running apps from the Home screen is the quickest way to shut them down.

What happens when you close an application?

What happens when you close apps on Android? Closing an app stops it from running in the background. It won't sign you out of any apps, but opening the app again will start a new instance. You will need to keep an app running in the background if you want to continue where you left off.

How do I see recently closed apps?

You'll be able to see your recent apps with a single tap. From the Home screen, tap the Recents icon to the left of the Home button. All of your active or opened apps will be listed. If you've customized your Navigation bar, Recents may be located on the right, unless you're using full screen gestures.


2 Answers

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:

  • Application.Exit() vs Application.ExitThread() vs Environment.Exit()
  • http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
like image 127
Naga Harish M Avatar answered Sep 18 '22 13:09

Naga Harish M


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.

like image 42
Justin Avatar answered Sep 18 '22 13:09

Justin