Four ways to close the current thread in C #.

Source: Internet
Author: User
Tags terminates

The. NET class library has helped us to close the window, and if this window is the main window of the system, closing this window should exit the entire application.
But in fact sometimes this is not the case, close the window, just stop the current window of the message loop.
The main system window, essentially the start message loop in the main function window, after the end of the message loop, the main function will basically complete the historical mission, the entire application will naturally end.
The Application.exit () method terminates the message loop on all threads, and in general, the program exits regardless of where this method is called.
But if you add some time-consuming or even dead-loop threads to your program, the program will still not end even if the message loop is terminated.
It is more appropriate to terminate all of your new threads before ending the message loop.
Sometimes we can't even figure out if the stubborn threads are executing, we need to use a bit of brute force.. NET gives us the Environment.exit (0) method that calls this method and the application forces an exit.

When you 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 you wrote the following code in the closed event of the word form:

private void Frmfiledisposal_closed (object sender, System.EventArgs e)
{
This. Dispose ();
This. Close ();
}

From the MSDN and from the online information can be known, the Dispose () method, although the resources of the current form can be freed, but can not force the end of the loop,
To force the highlighting of the current program:System.Environment.Exit (int exitcode) method : Terminates the current process and provides the specified exit code for the underlying operating system.

The problem is resolved 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:
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: System.Threading.Thread.CurrentThread.Abort ();
or process.getcurrentprocess (). Kill ()
or Application.exitthread ();
or Application.exitthread ()

However, the above method is forced to quit the entire program, not just close the subform. Some problems may also arise,
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 occupied by the child form


Application.exit (); Method stops all message loops running 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.

In Windows (Win32 or. NET), the correct way to close an application is to close its main application window (for example, Form.close). Any windows that still exist after the main message pump is closed need to be shut down manually. closing a window before the application exits by calling Form.close or Form.dispose is a good way to clear the window , but it requires you to do it consciously. We need to remember that the. NET Framework's onclosing () is a managed version of the Win32 wm_close, not Wm_destroy.

Also, if you use form. Close (), which allows your application to clean up content, close files, and so on, by handling onclosing or onclosed events. These events cannot be invoked if you forcibly quit the application through Application.exit.

Application.exit (); Method stops all message loops running 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 message pumps, expanding the call stack, and returning execution to the system
Environment.exit (0) method, calling this method, the application is forced to exit

Four ways to close the current thread in C #.

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.