Method for. Net to force exit the main window -- application. Exit () method and environment. Exit (0) Method

Source: Internet
Author: User
Tags terminates

The. NET class library has helped us close the window. If the window is the main window of the system, close the window and exit the entire application.Program.
But in fact, sometimes this is not the case. Close the window, but stop the message loop of the current window.
The main window of the system is essentially the window for starting the message loop in the main function. After the message loop ends, the main function basically completes its historical mission and the entire application naturally ends.
The application. Exit () method terminates the message loop on all threads. Generally, the program can exit wherever this method is called.
However, if you add some time-consuming or even endless threads to the program, the program will not end even if the message loop is terminated.
A moderate approach is to terminate all newly created threads before the message loop ends.
Sometimes we cannot even figure out whether or not the stubborn threads are being executed. We need to use some brute force means .. NET provides the environment. Exit (0) method for us. By calling this method, the application is forced to exit.

 

when you open a subform and perform an operation, if there is a loop operation in the word form. 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 you have written the following Code :< br>
private void frmfiledisposal_closed (Object sender, system. eventargs e)
{< br> This. dispose ();
This. close ();
}< br> the dispose () method can be used to release resources of the current form, but cannot force end the loop.
to forcibly highlight the current program, 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 following problem:
private void frmfiledisposal_closed (Object sender, system. eventargs e)
{< br> system. environment. exit (system. environment. exitcode);
This. dispose ();
This. close ();
}

or put it in the event:
while (MessageBox. Show ("exit the current form? "," ", Messageboxbuttons. yesno) = dialogresult. no)
{< br> for (Int J = 1; j <= I; j ++)
{< br> listbox1.items. add (j);
}< BR >}< br> system. environment. exit (system. environment. exitcode);

another method: system. threading. thread. currentthread. abort ();
or process. getcurrentprocess (). kill ()
or application. exitthread ();
or 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 resources occupied by sub-forms.

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.

In Windows (Win32 or. NET), the correct way to close an application is to close its main application window (such as form. Close ). You must manually close any windows that still exist after the master message pump ends. InIt is a good practice to clear a window by calling form. Close or form. Dispose before exiting the application.But you need to do it consciously. We need to remember that the onclosing () of the. NET Framework is wm_close of the Win32 managed version, rather than wm_destroy.

In addition, if you use form. Close (), you can clear the content and close the file of your application by handling the onclosing or onclosed events. If you use application. Exit to forcibly exit the application, these events cannot be called.

 

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.