ApplicationContext.ExitThread Method ()


Terminates the message loop of the thread.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void ExitThread()

This method calls ExitThreadCore.


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.

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.

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().
    if (formCount == 0) {

.NET Framework
Available since 1.1
Return to top