Best way to kill application instance

36,774

Solution 1

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.

Solution 2

If this is a Windows Forms application, use Application.Exit(). That will close the program nicely.

Share:
36,774
Eaton
Author by

Eaton

Updated on July 21, 2020

Comments

  • Eaton
    Eaton almost 4 years

    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?