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.
- 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.
Exampleprivate void btnClose_Click(object sender, EventArgs e) { this.close( ); } - 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.
Exampleprivate void btnClose_Click(object sender, EventArgs eventArgs) { System.Windows.Forms.Application.ExitThread( ); } - 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.
Exampleprivate void exitToolStripMenuItem_Click(object sender, EventArgs e) { System.Windows.Forms.Application.Exit( ) } - 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.
Examplepublic static void Main(string[] args) { System.Environment.Exit(0); }
I think this may be helpful to many developers.

por rezazadehPosted Jul 30, 2022, 10:47 AM
Your solution saved me
R.D IpPosted Apr 29, 2021, 4:28 AM
How to close thread form close event in c#
Robert BattamPosted Apr 26, 2021, 6:55 AM
Worked a charm, cheers!
Liridon AgushiPosted Aug 31, 2017, 3:26 AM
This.dispose(true); the best of all, and you didn't mention above !
Rupinder KangPosted Mar 21, 2017, 8:26 PM
I need who to CANCEL processing call c# winform If (ProductNameTX.Text =="") { return false; }
Sanghamitra MohantyPosted Dec 14, 2015, 7:09 AM
Nice one
Santhakumar MunuswamyPosted Oct 25, 2015, 11:43 AM
Good one
Sourabh SomaniPosted Oct 24, 2015, 11:07 AM
Good one :)
Sujeet SumanPosted Oct 24, 2015, 10:54 AM
Thanks to all of you.......
Francis SusaimichaelPosted Oct 22, 2015, 7:42 AM
Can we include break and return with this list?
Humayun Kabir MamunPosted Oct 22, 2015, 4:45 AM
Nice...
Dominique CejaPosted Oct 21, 2015, 4:00 PM
Great!
Priyaranjan K SPosted Oct 21, 2015, 1:05 PM
Thanks for the share
Akash VarshneyPosted Oct 21, 2015, 11:16 AM
Nice article
Nitin PanditPosted Oct 21, 2015, 11:16 AM
If I close the main form with this.close(); so my full application will close not only current form way so ,?
Nitin PanditPosted Oct 21, 2015, 11:16 AM
If I close the main form with this.close(); so my full application will close not only current form way so ,?
Del WoodcockPosted Oct 21, 2015, 10:46 AM
Great Information, Thanks!
Sibeesh VenuPosted Oct 21, 2015, 8:08 AM
Nice Share
Harshad PansuriyaPosted Oct 21, 2015, 7:52 AM
Nice one
Nilesh JadavPosted Oct 21, 2015, 6:37 AM
Nice one sir !!