Thread.Join メソッド

定義

このインスタンスが表すスレッドが終了するまで、呼び出し元のスレッドをブロックします。

オーバーロード

Join()

このインスタンスが表すスレッドが終了するまで、呼び出し元のスレッドをブロックします。標準 COM および SendMessage ポンピングの実行は継続されます。

Join(Int32)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。

Join(TimeSpan)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。

Join()

このインスタンスが表すスレッドが終了するまで、呼び出し元のスレッドをブロックします。標準 COM および SendMessage ポンピングの実行は継続されます。

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

例外

呼び出し元が、Unstarted 状態のスレッドを結合しようとしました。

待機中にスレッドが中断されました。

注釈

Join は、呼び出し元のスレッド (つまり、メソッドを呼び出すスレッド) をブロックする同期メソッドで、そのメソッドが呼び出されたスレッド Join が完了するまでです。 スレッドが終了したことを確認するには、このメソッドを使用します。 スレッドが終了しない場合、呼び出し元は無期限にブロックされます。 次の例では、スレッドは Thread1 の メソッドをJoin()呼び出します。これによりThread1、 が完了するまでThread2ブロックThread2されます。

using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         thread2.Join();
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
// The example displays output like the following:
//       Current thread: Thread1
//       
//       Current thread: Thread2
//       
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//       
//       
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
open System.Threading

let mutable thread1, thread2 =
    Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>

let threadProc () =
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"

    if
        Thread.CurrentThread.Name = "Thread1"
        && thread2.ThreadState <> ThreadState.Unstarted
    then
        thread2.Join()

    Thread.Sleep 4000
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
    printfn $"Thread1: {thread1.ThreadState}"
    printfn $"Thread2: {thread2.ThreadState}\n"

thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()

thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()

// The example displays output like the following:
//       Current thread: Thread1
//
//       Current thread: Thread2
//
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//
//
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
Imports System.Threading

Module Example
   Dim thread1, thread2 As Thread

   Public Sub Main()
      thread1 = new Thread(AddressOf ThreadProc)
      thread1.Name = "Thread1"
      thread1.Start()
      
      thread2 = New Thread(AddressOf ThreadProc)
      thread2.Name = "Thread2"
      thread2.Start()   
   End Sub

   Private Sub ThreadProc()
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      If (Thread.CurrentThread.Name = "Thread1" And 
          thread2.ThreadState <> ThreadState.Unstarted)
         thread2.Join()
      End If
      Thread.Sleep(4000)
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      Console.WriteLine("Thread1: {0}", thread1.ThreadState)
      Console.WriteLine("Thread2: {0}", thread2.ThreadState)
      Console.WriteLine()
   End Sub
End Module
' The example displays output like the following :
'       Current thread: Thread1
'       
'       Current thread: Thread2
'       
'       Current thread: Thread2
'       Thread1: WaitSleepJoin
'       Thread2: Running
'       
'       
'       Current thread: Thread1
'       Thread1: Running
'       Thread2: Stopped

が呼び出されたときに Join スレッドが既に終了している場合、 メソッドは直ちに を返します。

警告

現在のスレッドから Join 現在のスレッドを Thread 表す オブジェクトの メソッドを呼び出さないでください。 これにより、現在のスレッドがそれ自体を無期限に待機するため、アプリが応答しなくなります。

このメソッドは、 を含 ThreadState.WaitSleepJoinむように呼び出し元スレッドの状態を変更します。 状態のスレッドThreadState.Unstartedで を呼び出Joinすことはできません。

こちらもご覧ください

適用対象

Join(Int32)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。

public:
 bool Join(int millisecondsTimeout);
public bool Join (int millisecondsTimeout);
member this.Join : int -> bool
Public Function Join (millisecondsTimeout As Integer) As Boolean

パラメーター

millisecondsTimeout
Int32

スレッドが終了するまでの待機時間を表すミリ秒数。

戻り値

スレッドが終了した場合は truemillisecondsTimeout パラメーターで指定した時間が経過してもスレッドが終了していない場合は false

例外

millisecondsTimeout の値が負の値であり、Infinite (ミリ秒単位) と等しくありません。

スレッドは開始していません。

millisecondsTimeout は -1 未満です (Timeout.Infinite)。

待機中にスレッドが中断されました。

注釈

Join(Int32) は、呼び出し元スレッド (つまり、メソッドを呼び出すスレッド) をブロックする同期メソッドで、メソッドが呼び出されたスレッド Join が完了するか、タイムアウト間隔が経過するまでです。 次の例では、スレッドは Thread1 の メソッドをJoin()呼び出します。これによりThread1、完了するまで、または 2 秒が経過するまでThread2ブロックThread2されます。

using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         if (thread2.Join(2000))
            Console.WriteLine("Thread2 has termminated.");
         else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.");   
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
// The example displays the following output:
//       Current thread: Thread1
//       
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//       
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//       
//       
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
open System.Threading

let mutable thread1, thread2 =
    Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>

let threadProc () =
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"

    if
        Thread.CurrentThread.Name = "Thread1"
        && thread2.ThreadState <> ThreadState.Unstarted
    then
        if thread2.Join 2000 then
            printfn "Thread2 has termminated."
        else
            printfn "The timeout has elapsed and Thread1 will resume."

    Thread.Sleep 4000
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
    printfn $"Thread1: {thread1.ThreadState}"
    printfn $"Thread2: {thread2.ThreadState}\n"

thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()

thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()

// The example displays the following output:
//       Current thread: Thread1
//
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//
//
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
Imports System.Threading

Module Example
   Dim thread1, thread2 As Thread

   Public Sub Main()
      thread1 = new Thread(AddressOf ThreadProc)
      thread1.Name = "Thread1"
      thread1.Start()
      
      thread2 = New Thread(AddressOf ThreadProc)
      thread2.Name = "Thread2"
      thread2.Start()   
   End Sub

   Private Sub ThreadProc()
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      If (Thread.CurrentThread.Name = "Thread1" And 
          thread2.ThreadState <> ThreadState.Unstarted)
         If thread2.Join(TimeSpan.FromSeconds(2))
            Console.WriteLine("Thread2 has termminated.")
         Else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.")
         End If      
      End If
      Thread.Sleep(4000)
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      Console.WriteLine("Thread1: {0}", thread1.ThreadState)
      Console.WriteLine("Thread2: {0}", thread2.ThreadState)
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       Current thread: Thread1
'       
'       Current thread: Thread2
'       
'       Current thread: Thread2
'       Thread1: WaitSleepJoin
'       Thread2: Running
'       
'       
'       Current thread: Thread1
'       Thread1: Running
'       Thread2: Stopped

パラメーターに millisecondsTimeout が指定されている場合Timeout.Infinite、このメソッドは戻り値をJoin()除き、メソッドのオーバーロードと同じように動作します。

が呼び出されたときに Join スレッドが既に終了している場合、 メソッドは直ちに を返します。

このメソッドは、 を含 ThreadState.WaitSleepJoinむように呼び出し元スレッドの状態を変更します。 状態のスレッドThreadState.Unstartedで を呼び出Joinすことはできません。

こちらもご覧ください

適用対象

Join(TimeSpan)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。

public:
 bool Join(TimeSpan timeout);
public bool Join (TimeSpan timeout);
member this.Join : TimeSpan -> bool
Public Function Join (timeout As TimeSpan) As Boolean

パラメーター

timeout
TimeSpan

スレッドが終了するのを待機する時間に設定される TimeSpan

戻り値

スレッドが終了した場合は truetimeout パラメーターで指定した時間が経過してもスレッドが終了していない場合は false

例外

timeout 値は負の値で、ミリ秒単位に等しくない Infinite か、 Int32.MaxValue ミリ秒を超えています。

呼び出し元が、Unstarted 状態のスレッドを結合しようとしました。

次のコード例では、 メソッドで値を TimeSpan 使用する方法を Join 示します。

using namespace System;
using namespace System::Threading;

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

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

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( Test::Work ) );
   newThread->Start();
   if ( newThread->Join( waitTime + waitTime ) )
   {
      Console::WriteLine( "New thread terminated." );
   }
   else
   {
      Console::WriteLine( "Join timed out." );
   }
}
// The example displays the following output:
//        New thread terminated.
using System;
using System.Threading;

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

    public static void Main() 
    {
        Thread newThread = new Thread(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);
    }
}
// The example displays the following output:
//        New thread terminated.
open System
open System.Threading

let waitTime = TimeSpan(0, 0, 1)

let work () =
    Thread.Sleep waitTime

let newThread = Thread work
newThread.Start()

if waitTime + waitTime |> newThread.Join then
    printfn "New thread terminated."
else
    printfn "Join timed out."

// The example displays the following output:
//        New thread terminated.
Imports System.Threading

Public Module Test
    Dim waitTime As New TimeSpan(0, 0, 1)

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

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

    Private Sub Work()
        Thread.Sleep(waitTime)
    End Sub
End Module
' The example displays the following output:
'       New thread terminated.

注釈

Join(TimeSpan) は、呼び出し元スレッド (つまり、メソッドを呼び出すスレッド) をブロックする同期メソッドで、メソッドが呼び出されたスレッド Join が完了するか、タイムアウト間隔が経過するまでです。 次の例では、スレッドは Thread1 の メソッドをJoin()呼び出します。これによりThread1、完了するまで、または 2 秒が経過するまでThread2ブロックThread2されます。

using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         if (thread2.Join(TimeSpan.FromSeconds(2)))
            Console.WriteLine("Thread2 has termminated.");
         else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.");   
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
// The example displays the following output:
//       Current thread: Thread1
//       
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//       
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//       
//       
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
open System
open System.Threading

let mutable thread1, thread2 =
    Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>

let threadProc () =
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"

    if
        Thread.CurrentThread.Name = "Thread1"
        && thread2.ThreadState <> ThreadState.Unstarted
    then
        if TimeSpan.FromSeconds 2 |> thread2.Join then
            printfn "Thread2 has termminated."
        else
            printfn "The timeout has elapsed and Thread1 will resume."

    Thread.Sleep 4000
    printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
    printfn $"Thread1: {thread1.ThreadState}"
    printfn $"Thread2: {thread2.ThreadState}\n"

thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()

thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()

// The example displays the following output:
//       Current thread: Thread1
//
//       Current thread: Thread2
//       The timeout has elapsed and Thread1 will resume.
//
//       Current thread: Thread2
//       Thread1: WaitSleepJoin
//       Thread2: Running
//
//
//       Current thread: Thread1
//       Thread1: Running
//       Thread2: Stopped
Imports System.Threading

Module Example
   Dim thread1, thread2 As Thread

   Public Sub Main()
      thread1 = new Thread(AddressOf ThreadProc)
      thread1.Name = "Thread1"
      thread1.Start()
      
      thread2 = New Thread(AddressOf ThreadProc)
      thread2.Name = "Thread2"
      thread2.Start()   
   End Sub

   Private Sub ThreadProc()
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      If (Thread.CurrentThread.Name = "Thread1" And 
          thread2.ThreadState <> ThreadState.Unstarted)
         If thread2.Join(2000)
            Console.WriteLine("Thread2 has termminated.")
         Else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.")
         End If      
      End If
      Thread.Sleep(4000)
      Console.WriteLine()
      Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
      Console.WriteLine("Thread1: {0}", thread1.ThreadState)
      Console.WriteLine("Thread2: {0}", thread2.ThreadState)
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       Current thread: Thread1
'       
'       Current thread: Thread2
'       
'       Current thread: Thread2
'       Thread1: WaitSleepJoin
'       Thread2: Running
'       
'       
'       Current thread: Thread1
'       Thread1: Running
'       Thread2: Stopped

に がtimeout指定されている場合Timeout.Infinite、このメソッドは戻り値をJoin()除き、メソッドのオーバーロードと同じように動作します。

が呼び出されたときに Join スレッドが既に終了している場合、 メソッドは直ちに を返します。

このメソッドは、現在のスレッドの状態を を含 WaitSleepJoinむように変更します。 状態のスレッドThreadState.Unstartedで を呼び出Joinすことはできません。

こちらもご覧ください

適用対象