Export (0) Print
Expand All

Thread.Join Method ()

.NET Framework 1.1

Blocks the calling thread until a thread terminates.

[Visual Basic]
Overloads Public Sub Join()
[C#]
public void Join();
[C++]
public: void Join();
[JScript]
public function Join();

Exceptions

Exception Type Condition
ThreadStateException The caller attempted to join a thread that is in the ThreadState.Unstarted state.
ThreadInterruptedException The thread is interrupted while waiting.

Remarks

Use this method to ensure a thread has terminated. The caller will block indefinitely if the thread does not terminate.

This method changes the state of the calling thread to include ThreadState.WaitSleepJoin. You cannot invoke Join on a thread that is in the ThreadState.Unstarted state.

Example

[Visual Basic, C#, C++] The following code example shows how to use Join to wait for a thread to terminate.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.Threading

Public Class IsThreadPool

    Shared Sub Main()
        Dim autoEvent As New AutoResetEvent(False)

        Dim regularThread As New Thread(AddressOf ThreadMethod)
        regularThread.Start()
        ThreadPool.QueueUserWorkItem(AddressOf WorkMethod, autoEvent)

        ' Wait for foreground thread to end.
        regularThread.Join()

        ' Wait for background thread to end.
        autoEvent.WaitOne()
    End Sub

    Shared Sub ThreadMethod()
        Console.WriteLine("ThreadOne, executing ThreadMethod, " & _
            "is from the thread pool? {0}", _
            Thread.CurrentThread.IsThreadPoolThread)
    End Sub

    Shared Sub WorkMethod(stateInfo As Object)
        Console.WriteLine("ThreadTwo, executing WorkMethod, " & _
            "is from the thread pool? {0}", _
            Thread.CurrentThread.IsThreadPoolThread)

        ' Signal that this thread is finished.
        DirectCast(stateInfo, AutoResetEvent).Set()
    End Sub

End Class

[C#] 
using System;
using System.Threading;

class IsThreadPool
{
    static void Main()
    {
        AutoResetEvent autoEvent = new AutoResetEvent(false);

        Thread regularThread = 
            new Thread(new ThreadStart(ThreadMethod));
        regularThread.Start();
        ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), 
            autoEvent);

        // Wait for foreground thread to end.
        regularThread.Join();

        // Wait for background thread to end.
        autoEvent.WaitOne();
    }

    static void ThreadMethod()
    {
        Console.WriteLine("ThreadOne, executing ThreadMethod, " +
            "is {0}from the thread pool.", 
            Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
    }

    static void WorkMethod(object stateInfo)
    {
        Console.WriteLine("ThreadTwo, executing WorkMethod, " +
            "is {0}from the thread pool.", 
            Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");

        // Signal that this thread is finished.
        ((AutoResetEvent)stateInfo).Set();
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

__gc class IsThreadPool
{
public:
    static void ThreadMethod()
    {
        Console::WriteLine(S"ThreadOne, executing ThreadMethod, "
            S"is {0}from the thread pool.", 
            Thread::CurrentThread->IsThreadPoolThread ? S"" : S"not ");
    }

    static void WorkMethod(Object* stateInfo)
    {
        Console::WriteLine(S"ThreadTwo, executing WorkMethod, "
            S"is {0}from the thread pool.", 
            Thread::CurrentThread->IsThreadPoolThread ? S"" : S"not ");

        // Signal that this thread is finished.
        dynamic_cast<AutoResetEvent*>(stateInfo)->Set();
    }
};

void main()
{
    AutoResetEvent* autoEvent = new AutoResetEvent(false);

    Thread* regularThread = 
        new Thread(new ThreadStart(0, &IsThreadPool::ThreadMethod));
    regularThread->Start();
    ThreadPool::QueueUserWorkItem(new WaitCallback(0, 
        &IsThreadPool::WorkMethod), autoEvent);

    // Wait for foreground thread to end.
    regularThread->Join();

    // Wait for background thread to end.
    autoEvent->WaitOne();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Thread Class | Thread Members | System.Threading Namespace | Thread.Join Overload List | Thread States

Show:
© 2014 Microsoft