ApplicationContext.ExitThread 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
終止執行緒的訊息迴圈。
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
ExitThread 且 ExitThreadCore 不會實際導致執行緒終止。 這些方法 ThreadExit 提升了物件所聆聽的事件 Application 。 Application物件接著終止執行緒。