Export (0) Print
Expand All

Thread.Join Method (TimeSpan)

.NET Framework 1.1

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

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

Parameters

timeout
A TimeSpan set to the amount of time to wait for the thread to terminate.

Return Value

true if the thread terminated; false if the thread has not terminated after the amount of time specified by the timeout parameter has elapsed.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException The value of timeout is negative and is not equal to Timeout.Infinite in milliseconds, or is greater than MaxValue milliseconds.
ThreadStateException The caller attempted to join a thread that is in the Unstarted state.

Remarks

If Timeout.Infinite is specified for timeout, this method behaves identically to Join(), except for the return value.

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

Example

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

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

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