Environment.Exit Method (Int32)
Terminates this process and returns an exit code to the operating system.
Assembly: mscorlib (in mscorlib.dll)
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] public static void Exit( int exitCode )
The exit code to return to the operating system. Use 0 (zero) to indicate that the process completed successfully.
The caller does not have sufficient security permission to perform this function.
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 themethod differs from using your programming language's return statement in the following ways:
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.
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.
requires the caller to have permission to call unmanaged code. The return statement does not.
Ifis 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 constrained execution region (CER) is running, the CER will not complete execution. If the return statement is used, the CER completes execution.is called when code in a
Available since 1.1