Force exit application. Exit and environment. eixt of winform Program

Source: Internet
Author: User

the Program that processes a large amount of XML data generated by infopath over the past few days, I used the MDI subform, each form contains its own functions, such, traverse the directory and Its subdirectories to check the file type and automatically generate SQL statements and import them to the database, automatically check infopath data, replace and modify some fields in the data, and merge and split data files. remove records, statistical data, and automatically import data to the database... And so on.
when I open a subform and perform an operation, if the word form contains a loop operation. 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 the following Code :< br>
private void frmfiledisposal_closed (Object sender, system. eventargs e)
{< br> This. dispose ();
This. close ();
}< br> later, I learned from the query of msdn and online materials that the dispose () method can 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.

It seems that there are other methods, such as using system messages to notify and control the end of the loop. Haha, It is troublesome, so I won't be nagging here. If the expert has a better solution, hope you can give me some advice !!! Thank you in advance ^_^

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.