The exit WinForm program in C # includes a number of methods, such as: this. Close (); Application.exit (); Application.exitthread (); System.Environment.Exit (0);

Source: Internet
Author: User

This paper summarizes the techniques of WinForm program exit method in C #. Share to everyone for your reference. The specific analysis is as follows:

Exiting the WinForm program in C # includes a number of methods, such as: this. Close (); Application.exit (); Application.exitthread (); System.Environment.Exit (0); When their respective methods are different, let's take a look at the details below.

1.this.   Close (); Just close the current window, if it is not the main form, you can not quit the program, and if there is a managed thread (not the main threads), also can not cleanly exit;

2.application.exit (); Forces all messages to abort, exits all forms, but does not cleanly exit if there is a managed thread (not the main thread);

3.application.exitthread (); Force abort all messages on the calling thread, as well as the problem that other threads cannot exit correctly;

4.system.environment.exit (0); This is the most thorough exit method, no matter what threads are forced to quit, the program ends very clean.

See some examples below

When I open a subform and do an action, if there is a looping action in the word form. If I close the subform in the run, I find that the form is closed, but the loop program in the subform does not exit, executes, and consumes the system resources until the end of the loop to really release the resources. Although I wrote the following code in the closed event of the word form:

Copy CodeThe code is as follows: private void frmfiledisposal_closed (object sender, System.EventArgs e) {this.          Dispose (); This. Close (); }

Later from the MSDN and from the Internet to find out that the Dispose () method, although the resources of the current form can be freed, but not to force the end of the loop, to force the highlight of 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 underlying operating system.

The problem is resolved as follows:

Copy CodeThe code is as follows: private void frmfiledisposal_closed (object sender, System.EventArgs e) {System.Environment.Exit (System.envi Ronment.    ExitCode); This.  Dispose (); This. Close (); }

Or put it in the event:

Copy CodeThe 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);

There is another way:

Copy CodeThe code is as follows: System.Threading.Thread.CurrentThread.Abort ();

Or

Copy CodeThe code is as follows: Process.getcurrentprocess (). Kill ()

Or

Copy CodeThe code is as follows: Application.exitthread ();

Or

Copy CodeThe code is as follows: Application.exitthread ()

However, the above method is forced to quit the entire program, not just close the subform. Some issues may also be raised.

I feel the best way to do this is to write the loop exit condition in the closing event of the subform so that you can avoid some unknown errors, and you can just close the subform and release the resources that the subform occupies. Application.exit (); Method stops all message loops that run on all threads and closes all windows of the application. Application.exit is a form of forced exit, just like the PostQuitMessage () of Win32. It means discarding all the message pumps, expanding the call stack, and returning the execution to the system.

I hope this article is helpful to everyone's C # programming.

The exit WinForm program in C # includes a number of methods, such as: this. Close (); Application.exit (); Application.exitthread (); System.Environment.Exit (0);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.