Wednesday, 5 February 2020

C# - Form.Close() Vs Application.Exit() Vs Environment.Exit()

Form.Close Method
When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.
The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
--Note
When the Close method is called on a Form displayed as a modeless window, you cannot call the Show method to make the form visible, because the form's resources have already been released. To hide a form and then make it visible, use the Control.Hide method.
--Caution
Prior to the .NET Framework 2.0, the Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Application.Exit Method
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Overloads
Exit()
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Exit(CancelEventArgs)
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Environment.Exit(Int32) Method
Terminates this process and returns an exit code to the operating system.
Remarks
For the exitCode parameter, use a non-zero number to indicate an error. In your application, you can define your own error codes in an enumeration, and return the appropriate error code based on the scenario. For example, return a value of 1 to indicate that the required file is not present, and a value of 2 to indicate that the file is in the wrong format. For a list of exit codes used by the Windows operating system, see System Error Codes in the Windows documentation.
Calling the Exit method differs from using your programming language's return statement in the following ways:
Exit always terminates an application. Using the return statement may terminate an application only if it is used in the application entry point, such as in the Main method.
Exit terminates an application immediately, even if other threads are running. If the return statement is called in the application entry point, it causes an application to terminate only after all foreground threads have terminated.
Exit requires the caller to have permission to call unmanaged code. The return statement does not.
If Exit is called from a try or catch block, the code in any finally block does not execute. If the return statement is used, the code in the finally block does execute.
If Exit is called when code in a constrained execution region (CER) is running, the CER will not complete execution. If the return statement is used, the CER completes execution.

No comments:

Post a Comment