Export (0) Print
Expand All
4 out of 10 rated this helpful - Rate this topic

Thread.Join Method

Blocks the calling thread until a thread terminates.

Overload List

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();

Blocks the calling thread until a thread terminates or the specified time elapses.

[Visual Basic] Overloads Public Function Join(Integer) As Boolean
[C#] public bool Join(int);
[C++] public: bool Join(int);
[JScript] public function Join(int) : Boolean;

Blocks the calling thread until a thread terminates or the specified time elapses.

[Visual Basic] Overloads Public Function Join(TimeSpan) As Boolean
[C#] public bool Join(TimeSpan);
[C++] public: bool Join(TimeSpan);
[JScript] public function Join(TimeSpan) : Boolean;

Example

[Visual Basic, C#, C++] The following code example demonstrates how to use a TimeSpan value with the Join method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Join. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Threading

Public Class Test

    Shared waitTime As New TimeSpan(0, 0, 1)

    Shared Sub Main() 
        Dim newThread As New Thread(AddressOf Work)
        newThread.Start()

        If newThread.Join( _
            TimeSpan.op_Addition(waitTime, waitTime)) Then

            Console.WriteLine("New thread terminated.")
        Else
            Console.WriteLine("Join timed out.")
        End If
    End Sub

    Shared Sub Work()
        Thread.Sleep(waitTime)
    End Sub

End Class

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

class Test
{
    static TimeSpan waitTime = new TimeSpan(0, 0, 1);

    public static void Main() 
    {
        Thread newThread = 
            new Thread(new ThreadStart(Work));
        newThread.Start();

        if(newThread.Join(waitTime + waitTime))
        {
            Console.WriteLine("New thread terminated.");
        }
        else
        {
            Console.WriteLine("Join timed out.");
        }
    }

    static void Work()
    {
        Thread.Sleep(waitTime);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

static TimeSpan waitTime = TimeSpan(0, 0, 1);

__gc class Test
{
public:
    static void Work()
    {
        Thread::Sleep(waitTime);
    }
};

void main() 
{
    Thread* newThread = new Thread(new ThreadStart(0, Test::Work));
    newThread->Start();

    if(newThread->Join(waitTime + waitTime))
    {
        Console::WriteLine(S"New thread terminated.");
    }
    else
    {
        Console::WriteLine(S"Join timed out.");
    }
}

[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.

See Also

Thread Class | Thread Members | System.Threading Namespace

Show:
© 2014 Microsoft. All rights reserved.