Environment.Exit(Int32) 方法

定義

結束這個處理序,並傳回結束代碼給作業系統。Terminates this process and returns an exit code to the operating system.

public:
 static void Exit(int exitCode);
public static void Exit (int exitCode);
static member Exit : int -> unit
Public Shared Sub Exit (exitCode As Integer)

參數

exitCode
Int32

要傳回作業系統的結束代碼。The exit code to return to the operating system. 使用 0 (零) 表示處理程序已順利完成。Use 0 (zero) to indicate that the process completed successfully.

例外狀況

呼叫端沒有足夠的安全性權限執行這項功能。The caller does not have sufficient security permission to perform this function.

備註

若為 exitCode 參數,請使用非零的數位來表示錯誤。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. 例如,會傳回1的值,表示所需的檔案不存在,而值為2則表示檔案的格式錯誤。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. 如需 Windows 作業系統所使用的結束代碼清單,請參閱 Windows 檔中的系統錯誤碼For a list of exit codes used by the Windows operating system, see System Error Codes in the Windows documentation.

呼叫 Exit 方法與在下列方式中使用程式設計語言的 return 語句不同:Calling the Exit method differs from using your programming language's return statement in the following ways:

  • Exit 一律會終止應用程式。Exit always terminates an application. 只有當應用程式用於應用程式進入點(例如,在 Main 方法中)時,才能使用 return 語句來終止它。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 會立即終止應用程式,即使其他執行緒正在執行中也一樣。Exit terminates an application immediately, even if other threads are running. 如果在應用程式進入點中呼叫 return 語句,則只有在所有前景執行緒都終止之後,應用程式才會終止。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 需要呼叫端具有呼叫非受控碼的許可權。Exit requires the caller to have permission to call unmanaged code. @No__t-0 語句則不會。The return statement does not.

  • 如果從 try 或 @no__t 2 區塊呼叫 Exit,則不會執行任何 finally 區塊中的程式碼。If Exit is called from a try or catch block, the code in any finally block does not execute. 如果使用 return 語句,則會執行 finally 區塊中的程式碼。If the return statement is used, the code in the finally block does execute.

  • 如果在限制執列區域(CER)中的程式碼正在執行時呼叫 Exit,則 CER 不會完成執行。If Exit is called when code in a constrained execution region (CER) is running, the CER will not complete execution. 如果使用 return 語句,則 CER 會完成執行。If the return statement is used, the CER completes execution.

安全性

SecurityPermission
提供呼叫非受控碼的能力。for the ability to call unmanaged code. 相關聯的列舉: UnmanagedCodeAssociated enumeration: UnmanagedCode

適用於