Thread.Join メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
このインスタンスによって表されるスレッドが終了するまで、呼び出し元のスレッドをブロックします。
オーバーロード
| 名前 | 説明 |
|---|---|
| Join() |
このインスタンスによって表されるスレッドが終了するまで呼び出し元のスレッドをブロックし、標準の COM と |
| Join(Int32) |
このインスタンスによって表されるスレッドが終了するか、指定された時間が経過するまで呼び出し元のスレッドをブロックします。一方で、標準の COM および SendMessage ポンプを引き続き実行します。 |
| Join(TimeSpan) |
このインスタンスによって表されるスレッドが終了するか、指定された時間が経過するまで呼び出し元のスレッドをブロックします。一方で、標準の COM および SendMessage ポンプを引き続き実行します。 |
Join()
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
このインスタンスによって表されるスレッドが終了するまで呼び出し元のスレッドをブロックし、標準の COM と SendMessage ポンプを引き続き実行します。
public:
void Join();
public void Join();
member this.Join : unit -> unit
Public Sub Join ()
例外
呼び出し元が、 Unstarted 状態のスレッドに参加しようとしました。
待機中にスレッドが中断されます。
注釈
Join は、呼び出し元のスレッド (つまり、メソッドを呼び出すスレッド) を、 Join メソッドが呼び出されるスレッドが完了するまでブロックする同期メソッドです。 スレッドが終了したことを確認するには、このメソッドを使用します。 スレッドが終了しない場合、呼び出し元は無期限にブロックされます。 次の例では、Thread1 スレッドはJoin()のThread2 メソッドを呼び出します。これにより、Thread1が完了するまで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が呼び出されたときにスレッドが既に終了している場合、メソッドはすぐに戻ります。
Warning
現在のスレッドから現在のスレッドを表すJoin オブジェクトのThread メソッドを呼び出さないでください。 これにより、現在のスレッドがそれ自体を無期限に待機するため、アプリが応答しなくなります。
このメソッドは、呼び出し元のスレッドの状態を変更して、 ThreadState.WaitSleepJoinを含めます。
Join状態のスレッドでThreadState.Unstartedを呼び出すことはできません。
こちらもご覧ください
適用対象
Join(Int32)
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
このインスタンスによって表されるスレッドが終了するか、指定された時間が経過するまで呼び出し元のスレッドをブロックします。一方で、標準の 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
スレッドが終了するまで待機するミリ秒数。
返品
trueスレッドが終了した場合。falsemillisecondsTimeout パラメーターで指定された時間が経過した後にスレッドが終了していない場合に使用します。
例外
millisecondsTimeoutの値は負の値であり、ミリ秒単位のInfiniteと等しくありません。
スレッドが開始されていません。
millisecondsTimeout が -1 (Timeout.Infinite) 未満です。
待機中にスレッドが中断されました。
注釈
Join(Int32) は、呼び出し元のスレッド (つまり、メソッドを呼び出すスレッド) を、 Join メソッドが呼び出されたスレッドが完了するか、タイムアウト間隔が経過するまでブロックする同期メソッドです。 次の例では、Thread1 スレッドはJoin()のThread2 メソッドを呼び出します。これにより、Thread1が完了するか 2 秒が経過するまで、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
Timeout.Infinite パラメーターにmillisecondsTimeoutが指定されている場合、このメソッドは戻り値を除き、Join() メソッドのオーバーロードと同じように動作します。
Joinが呼び出されたときにスレッドが既に終了している場合、メソッドはすぐに戻ります。
このメソッドは、呼び出し元のスレッドの状態を変更して、 ThreadState.WaitSleepJoinを含めます。
Join状態のスレッドでThreadState.Unstartedを呼び出すことはできません。
こちらもご覧ください
適用対象
Join(TimeSpan)
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
- ソース:
- Thread.cs
このインスタンスによって表されるスレッドが終了するか、指定された時間が経過するまで呼び出し元のスレッドをブロックします。一方で、標準の 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
パラメーター
返品
trueスレッドが終了した場合。falsetimeout パラメーターで指定された時間が経過した後にスレッドが終了していない場合に使用します。
例外
timeoutの値は負の値であり、ミリ秒単位のInfiniteと等しくないか、Int32.MaxValue ミリ秒より大きい値です。
呼び出し元が、 Unstarted 状態のスレッドに参加しようとしました。
例
次のコード例では、TimeSpan メソッドでJoin値を使用する方法を示します。
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()のThread2 メソッドを呼び出します。これにより、Thread1が完了するか 2 秒が経過するまで、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.Infiniteにtimeoutが指定されている場合、このメソッドは戻り値を除き、Join() メソッドのオーバーロードと同じように動作します。
Joinが呼び出されたときにスレッドが既に終了している場合、メソッドはすぐに戻ります。
このメソッドは、現在のスレッドの状態を変更して、 WaitSleepJoinを含めます。
Join状態のスレッドでThreadState.Unstartedを呼び出すことはできません。