In the past few days, I am working on a program to process a large amount of XML data generated by Infopath. I have used an 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 I write the following code in the word form Closed event:
Private void frmFileDisposal_Closed (object sender, System. EventArgs e)
{
This. Dispose ();
This. Close ();
}
Later, I learned from the query of MSDN and online materials that, although the Dispose () method can release the resources of the current form, it cannot forcibly end the cycle,
To force the current program to use the: System. Environment. Exit (int exitcode) method, terminate the current process and provide the specified Exit code for the basic operating System.
Solve the problem 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 );
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. Calling Form. Close or Form. Dispose to Close a window before exiting the application is a good practice to clear the window, but you need to do this 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.