Exit Methods In C# Application

Many times we are confused about what is an appropriate way to exit inside an application. So I have tried to explain below the different exit methods in C# & their use.

  1. this.Close( )

    When we need to exit or close opened form then we should use "this.Close( )" method to close the form on some button click event.

    Example
    private void btnClose_Click(object sender, EventArgs e)  
    {  
       this.close( );  
    }
  2. System.Windows.Forms.Application.ExitThread( )

    When we are running a winform application & need to exit or close SUB APPLICATION or CURRENT THREAD then we should use "System.Windows.Forms.Application.ExitThread( )". Before you exit your application, you really need to have all your other thread exited, unless they are background threads or the threads obtained via the ThreadPool. This method exits the message loop on the current thread and closes all windows on the thread.

    Example
    private void btnClose_Click(object sender, EventArgs eventArgs)  
    {  
       System.Windows.Forms.Application.ExitThread( );  
    }
  3. System.Windows.Forms.Application.Exit( )

    When we are running a winform application & need to exit or close ENTIRE APPLICATION then we should use "System.Windows.Forms.Application.Exit( )". This method internally informs all message loops in application that you must terminate. Then this method wait to close all application windows till the message loops have been processed. This method does not force the application to exit whereas this method force RUN method (we are calling run method inside program file) to return.

    Example
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)  
    {  
       System.Windows.Forms.Application.Exit( )  
    }
  4. System.Environment.Exit(a_ExitCode)

    When we are running a console application & need to exit or close whole application then we should use "System.Environment.Exit(a_ExitCode)" where this exit code is an int type argument, which show the status of process. This method terminates this process and gives the underlying operating system the specified exit code. We should not use this method in winform application because winform application have some running message loops.

    Note:

    a_ExitCode: If in your application main method return type is void, then you should use this property to assign the exit code value. This exit code value will be returned to the calling process. If your main method return something, then you should ignore this. The default value of this property is zero, which shows that the process completed successfully. You can use other number to indicate an error like 1, 2 , 3, 4, 5, ........ till 499. Also you can create some error codes yourself & use in your application that will return the suitable error code.

    Example
    public static void Main(string[] args)  
    {  
       System.Environment.Exit(0);  
    }

I think this may be helpful to many developers.


Recommended Free Ebook
Similar Articles