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

注意

ExitThreadExitThreadCore 實際上不會造成執行緒終止。 這些方法會引發 ThreadExit 物件所接聽的事件 Application 。 然後 Application 物件會終止執行緒。

適用於