C # Force quit a program

Source: Internet
Author: User

Reference: http://blog.csdn.net/tanhua103292/article/details/4283203

1. Application.exit and Environment.eixt of the mandatory exit WinForm program

MSDN and the data from the Internet only to know that 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.

< Span style= "margin:0px; padding:0px; Color: #0000ff; " 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);

< Span style= "margin:0px; padding:0px; Color: #0000ff; " >2. 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 that the subform occupies.

3. 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.

C # Force quit a program

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.