Exit of the program in C #

Source: Internet
Author: User
Tags exit in

Program shutdown is very exquisite. If the processing is not good, the software will be turned on and off continuously. When the software is started several times, the program will crash. Or program exit is slow. Details determine the success or failure. A good software should be strictly repeated in all aspects, and strive to be impeccable.
Some programmers like to let the program do some things when it is closed, such as saving some records or something. I think this is not necessary, because these will affect the exit time, the single responsibility principle has repeatedly stressed the need for a single responsibility. Closing a program is to close the program, and do not add other actions.
Of course, there are also many ways to Close the form, such as Close (), Application. exit (), Application. exitThread (), Enviroment. exit (0), and Process. getCurrentProcess (). kill (). These are believed to have been used during exit. All these methods can exit the program. In my initial project, since there was no comprehensive design at the beginning, many features were added with new ideas, so a large number of threads were used, if you have a name or no name, you can manage it. If you do not have a name, you cannot obtain it and manage it. When exiting the subwindow, the current Form is generally used. close (), then obtain the threads in the window, and then Abort () one by one, but when exiting the main program, this. close (), it is very slow, because many threads are still running. So the method of killing the process was used at that time.
I have used all of the preceding exit methods.

[Csharp]
<Strong> Close () </strong>: Close the main form. However, when this function is disabled, the program calls the Dispose method, that is, in the Designer of the form. In this way, resources of the current form are released, however, the loop cannot be forcibly ended. If a thread is running in the program, it takes a long time to close the loop.
<Strong> Application. Exit () </strong>. It takes a long time for a thread to exist.
<Strong> Application. ExitThread () </strong> to exit the message loop on the current thread and close the window of the thread.
<Strong> Enviroment. Exit (0) </strong>. This method is forced to Exit.
<Strong> Process. getCurrentProcess (). kill () </strong>: If you think that other methods do not exit in a timely manner or fail, this method is the ultimate magic weapon. You can directly Kill the process and, of course, exit.
<Strong> Close () </strong>: Close the main form. However, when this function is disabled, the program calls the Dispose method, that is, in the Designer of the form. In this way, resources of the current form are released, however, the loop cannot be forcibly ended. If a thread is running in the program, it takes a long time to close the loop.
<Strong> Application. Exit () </strong>. It takes a long time for a thread to exist.
<Strong> Application. ExitThread () </strong> to exit the message loop on the current thread and close the window of the thread.
<Strong> Enviroment. Exit (0) </strong>. This method is forced to Exit.
<Strong> Process. getCurrentProcess (). kill () </strong>: If you think that other methods do not exit in a timely manner or fail, this method is the ultimate magic weapon. You can directly Kill the process and, of course, exit.
Of course, when the program exits, the program itself needs to process a lot of resources. The safest way is to Close the method in the main form and then hand over the rest to the program. Although the exit is slow, however, it is absolutely safe to exit the program through its own mechanism. But will the user allow you to do this? If your software click to close and the result remains in the process for a long time, or you still cannot release resources for half a day, the user may not use your program any more. Therefore, in line with the principle of all for the user, it is better to exit quickly. My common method is.

[Csharp]
Form. Close ();
Process. GetCurrentProcess (). Kill ();
Form. Close ();
Process. GetCurrentProcess (). Kill ();
Use it at the same time.

 

From Poplar

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.