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。
注释
ExitThread 并且 ExitThreadCore 实际上不会导致线程终止。 这些方法将引发 ThreadExit 对象侦听的事件 Application 。 然后,该 Application 对象将终止线程。