Summary of WinForm program exit methods in C # [conversion],
This article mainly introduces the WinForm exit method in C #. The example summarizes various common skills used to exit the WinForm program window, which is very useful. For more information, see
This example summarizes the WinForm exit method in C. Share it with you for your reference. The specific analysis is as follows:
There are many methods to exit the WinForm program in c #, such as: this. close (); Application. exit (); Application. exitThread (); System. environment. exit (0); when their respective methods are different, we will introduce them in detail.
1. this. close (); only closes the current window. If it is not the main form, it cannot exit the program. In addition, if there is a managed thread (not the main thread), it cannot exit cleanly;
2. Application. Exit (); force all messages to stop and Exit all forms. However, if there is a managed thread (not the main thread), it cannot Exit cleanly;
3. Application. ExitThread (); force stop all messages on the calling thread, which also faces the problem that other threads cannot exit correctly;
4. System. Environment. Exit (0); this is the most thorough Exit method. No matter what thread is forced to Exit, the program ends very cleanly.
See some examples below
When I open a subform and perform an operation, if the word form contains a loop operation. If I close the subform while running, I find that the subform is closed, but the loop program in the subform is not exited, it is being executed and occupies system resources until the loop ends, to release resources. Although I write the following code in the word form Closed event:
The Code is as follows:
private void frmFileDisposal_Closed(object sender, System.EventArgs e){ this.Dispose(); this.Close();}
Later, I learned from the query of MSDN and online materials that the Dispose () method, although it can release the resources of the current form, cannot force the end of the cycle, to force highlight the current program to use: System. environment. exit (int exitcode) method. This method terminates the current process and provides the specified Exit code for the basic operating system.
Solve the problem as follows:
The Code is as follows:
private void frmFileDisposal_Closed(object sender, System.EventArgs e){ System.Environment.Exit(System.Environment.ExitCode); this.Dispose(); this.Close();}
Or put it in the event:
The Code is as follows:
While (MessageBox. Show ("exit the current form? "," ", MessageBoxButtons. yesNo) = DialogResult. no) {for (int j = 1; j <= I; j ++) {listBox1.Items. add (j) ;}} System. environment. exit (System. environment. exitCode );
The Code is as follows:
System.Threading.Thread.CurrentThread.Abort();
Or
The Code is as follows:
Process.GetCurrentProcess().Kill()
Or
The Code is as follows:
Application.ExitThread();
Or
The Code is as follows:
Application.ExitThread()
However, the above methods force the entire program to exit, not just close the child form. Some problems may occur.
I think the best way is to write the loop exit condition in the Closing event of the child form to avoid some unknown errors, and only close the child form, and release the resources occupied by the sub-form.
Application. Exit (); Method to stop all message loops running on all threads and close all windows of the Application.
Application. Exit is a forced Exit method, just like Win32 PostQuitMessage (). It means to discard all message pumps, expand the call stack, and return the execution to the system.
I hope this article will help you with C # programming.