ApplicationContext.ExitThread Method

Definition

Terminates the message loop of the thread.

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

Examples

The following code example is an excerpt from the example in the ApplicationContext class overview. This example keeps track of open forms and exits the current thread when all forms are closed. The OnFormClosed method is an event handler for the Closed event. When the number of open forms is equal to 0, then current thread is exited by calling the ExitThread method. The number of forms is tracked by incrementing the formCount variable when a form is shown, and decrementing it when a form is closed.

Some code is not shown for the purpose of brevity. See ApplicationContext for the whole code listing.

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

Remarks

This method calls ExitThreadCore.

Note

ExitThread and ExitThreadCore do not actually cause the thread to terminate. These methods raise the ThreadExit event to which the Application object listens. The Application object then terminates the thread.

Applies to