Export (0) Print
Expand All
3 out of 11 rated this helpful - Rate this topic

Environment.Exit Method

Terminates this process and gives the underlying operating system the specified exit code.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void Exit(
	int exitCode
)

Parameters

exitCode
Type: System.Int32

Exit code to be given to the operating system. Use 0 (zero) to indicate that the process completed successfully.

ExceptionCondition
SecurityException

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 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 finally block, the code in any catch block does not execute. If the return statement is used, the code in the catch block does execute.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.