Thread.Join メソッド

定義

このインスタンスが表すスレッドが終了するまで、呼び出し元のスレッドをブロックします。Blocks the calling thread until the thread represented by this instance terminates.

オーバーロード

Join()

このインスタンスが表すスレッドが終了するまで、呼び出し元のスレッドをブロックします。標準 COM および SendMessage ポンピングの実行は継続されます。Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM and SendMessage pumping.

Join(Int32)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。Blocks the calling thread until the thread represented by this instance terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.

Join(TimeSpan)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。Blocks the calling thread until the thread represented by this instance terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.

Join()

このインスタンスが表すスレッドが終了するまで、呼び出し元のスレッドをブロックします。標準 COM および SendMessage ポンピングの実行は継続されます。Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM and SendMessage pumping.

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

例外

呼び出し元が、Unstarted 状態のスレッドを結合しようとしました。The caller attempted to join a thread that is in the Unstarted state.

待機中にスレッドが中断されました。The thread is interrupted while waiting.

注釈

Join は、Join メソッドが呼び出されたスレッドが完了するまで、呼び出し元のスレッド (つまり、メソッドを呼び出すスレッド) をブロックする同期メソッドです。Join is a synchronization method that blocks the calling thread (that is, the thread that calls the method) until the thread whose Join method is called has completed. スレッドが終了したことを確認するには、このメソッドを使用します。Use this method to ensure that a thread has been terminated. スレッドが終了しない場合、呼び出し元は無期限にブロックされます。The caller will block indefinitely if the thread does not terminate. 次の例では、Thread1 スレッドが Thread2Join() メソッドを呼び出します。これにより、Thread2 が完了するまで Thread1 がブロックされます。In the following example, the Thread1 thread calls the Join() method of Thread2, which causes Thread1 to block until Thread2 has completed.

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
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 が呼び出されたときにスレッドが既に終了している場合、メソッドはすぐに制御を戻します。If the thread has already terminated when Join is called, the method returns immediately.

警告

現在のスレッドの現在のスレッドを表す Thread オブジェクトの Join メソッドを呼び出すことはできません。You should never call the Join method of the Thread object that represents the current thread from the current thread. これにより、現在のスレッドが無期限に待機しているため、アプリが応答しなくなることがあります。This causes your app to become unresponsive because the current thread waits upon itself indefinitely,

このメソッドは、呼び出し元のスレッドの状態を変更して ThreadState.WaitSleepJoin を含めます。This method changes the state of the calling thread to include ThreadState.WaitSleepJoin. ThreadState.Unstarted 状態のスレッドで Join を呼び出すことはできません。You cannot invoke Join on a thread that is in the ThreadState.Unstarted state.

こちらもご覧ください

Join(Int32)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。Blocks the calling thread until the thread represented by this instance terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.

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

スレッドが終了するまでの待機時間を表すミリ秒数。The number of milliseconds to wait for the thread to terminate.

戻り値

スレッドが終了した場合は truemillisecondsTimeout パラメーターで指定した時間が経過してもスレッドが終了していない場合は falsetrue if the thread has terminated; false if the thread has not terminated after the amount of time specified by the millisecondsTimeout parameter has elapsed.

例外

millisecondsTimeout の値が負の値であり、Infinite (ミリ秒単位) と等しくありません。The value of millisecondsTimeout is negative and is not equal to Infinite in milliseconds.

スレッドは開始していません。The thread has not been started.

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

待機中にスレッドが中断されました。The thread was interrupted while waiting.

注釈

Join(Int32) は、Join メソッドが呼び出されたスレッドが完了するか、タイムアウト間隔が経過するまで、呼び出し元スレッド (つまり、メソッドを呼び出すスレッド) をブロックする同期メソッドです。Join(Int32) is a synchronization method that blocks the calling thread (that is, the thread that calls the method) until either the thread whose Join method is called has completed or the time-out interval has elapsed. 次の例では、Thread1 スレッドが Thread2Join() メソッドを呼び出します。これにより、Thread2 が完了するか、2秒が経過するまで、Thread1 がブロックされます。In the following example, the Thread1 thread calls the Join() method of Thread2, which causes Thread1 to block either until Thread2 has completed or 2 seconds have elapsed.

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
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() メソッドのオーバーロードと同じように動作します。If Timeout.Infinite is specified for the millisecondsTimeout parameter, this method behaves identically to the Join() method overload, except for the return value.

Join が呼び出されたときにスレッドが既に終了している場合、メソッドはすぐに制御を戻します。If the thread has already terminated when Join is called, the method returns immediately.

このメソッドは、呼び出し元のスレッドの状態を変更して ThreadState.WaitSleepJoin を含めます。This method changes the state of the calling thread to include ThreadState.WaitSleepJoin. ThreadState.Unstarted 状態のスレッドで Join を呼び出すことはできません。You cannot invoke Join on a thread that is in the ThreadState.Unstarted state.

こちらもご覧ください

Join(TimeSpan)

このインスタンスが表すスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。Blocks the calling thread until the thread represented by this instance terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.

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

スレッドが終了するのを待機する時間に設定される TimeSpanA TimeSpan set to the amount of time to wait for the thread to terminate.

戻り値

スレッドが終了した場合は truetimeout パラメーターで指定した時間が経過してもスレッドが終了していない場合は falsetrue if the thread terminated; false if the thread has not terminated after the amount of time specified by the timeout parameter has elapsed.

例外

timeout の値が負の値で、Infinite (ミリ秒単位) と等しくないか、または MaxValue ミリ秒を超えています。The value of timeout is negative and is not equal to Infinite in milliseconds, or is greater than MaxValue milliseconds.

呼び出し元が、Unstarted 状態のスレッドを結合しようとしました。The caller attempted to join a thread that is in the Unstarted state.

次のコード例は、Join メソッドで TimeSpan 値を使用する方法を示しています。The following code example demonstrates how to use a TimeSpan value with the Join method.

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.
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 メソッドが呼び出されたスレッドが完了するか、タイムアウト間隔が経過するまで、呼び出し元スレッド (つまり、メソッドを呼び出すスレッド) をブロックする同期メソッドです。Join(TimeSpan) is a synchronization method that blocks the calling thread (that is, the thread that calls the method) until either the thread whose Join method is called has completed or the time-out interval has elapsed. 次の例では、Thread1 スレッドが Thread2Join() メソッドを呼び出します。これにより、Thread2 が完了するか、2秒が経過するまで、Thread1 がブロックされます。In the following example, the Thread1 thread calls the Join() method of Thread2, which causes Thread1 to block either until Thread2 has completed or 2 seconds have elapsed.

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
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() メソッドのオーバーロードと同じように動作します。If Timeout.Infinite is specified for timeout, this method behaves identically to the Join() method overload, except for the return value.

Join が呼び出されたときにスレッドが既に終了している場合、メソッドはすぐに制御を戻します。If the thread has already terminated when Join is called, the method returns immediately.

このメソッドは、現在のスレッドの状態を変更して WaitSleepJoin を含めます。This method changes the state of the current thread to include WaitSleepJoin. ThreadState.Unstarted 状態のスレッドで Join を呼び出すことはできません。You cannot invoke Join on a thread that is in the ThreadState.Unstarted state.

こちらもご覧ください

適用対象