ApplicationContext.ExitThread 方法

定義

終止執行緒的訊息迴圈。

public:
 void ExitThread();
public void ExitThread();
member this.ExitThread : unit -> unit
Public Sub ExitThread ()

範例

以下程式碼範例摘自課程概述中的 ApplicationContext 範例。 此範例追蹤開啟表單,並在所有表單關閉後退出當前執行緒。 這個 OnFormClosed 方法是事件 Closed 的事件處理程序。 當開啟表單數為 0 時,呼叫 ExitThread 該方法即可退出當前執行緒。 表單數量是透過在顯示表單時增加 formCount 變數,閉合時遞減變數來追蹤。

部分程式碼未顯示,以求簡潔。 完整程式碼列表請見 ApplicationContext

void OnFormClosed( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   
   // When a form is closed, decrement the count of open forms.
   // When the count gets to 0, exit the app by calling
   // ExitThread().
   _formCount--;
   if ( _formCount == 0 )
   {
      ExitThread();
   }
}
private void OnFormClosed(object sender, EventArgs e)
{
    // When a form is closed, decrement the count of open forms.

    // When the count gets to 0, exit the app by calling
    // ExitThread().
    _formCount--;
    if (_formCount == 0)
    {
        ExitThread();
    }
}
Private Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
    ' When a form is closed, decrement the count of open forms.

    ' When the count gets to 0, exit the app by calling
    ' ExitThread().
    _formCount = _formCount - 1
    If (_formCount = 0) Then
        ExitThread()
    End If
End Sub

備註

此方法會呼叫 ExitThreadCore

Note

ExitThreadExitThreadCore 不會實際導致執行緒終止。 這些方法 ThreadExit 提升了物件所聆聽的事件 ApplicationApplication物件接著終止執行緒。

適用於