Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to kill application instance

What is the best way to kill an application instance? I am aware of these three methods:

  1. Application.Exit()

  2. Environment.Exit(0)

  3. Process.GetCurrentProcess().Kill()

Can anyone tell me which is better or when using each of the above would be appropriate?

like image 860
Eaton Avatar asked Oct 25 '10 23:10

Eaton


1 Answers

guidelines from c# faq:

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 WinForms 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.

Killing the process is likely not recommended.

like image 192
Brandon Frohbieter Avatar answered Sep 21 '22 22:09

Brandon Frohbieter