Thread.Join メソッド ()
アセンブリ: mscorlib (mscorlib.dll 内)



![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
このメソッドを使用して、スレッドが終了したかどうかを確認します。スレッドが終了していない場合、呼び出し元は無制限にブロックされます。Join の呼び出し時にそのスレッドが既に終了している場合は、そのメソッドから制御がすぐに戻ります。
このメソッドは、呼び出し元のスレッドの状態を変更して、ThreadState.WaitSleepJoin の状態が含まれるようにします。ThreadState.Unstarted 状態のスレッドで Join を呼び出すことはできません。

Join を使用してスレッドが終了するのを待機する方法の例を次に示します。
Option Explicit Option Strict Imports System Imports System.Threading Public Class IsThreadPool <MTAThread> _ Shared Sub Main() Dim autoEvent As New AutoResetEvent(False) Dim regularThread As New Thread(AddressOf ThreadMethod) regularThread.Start() ThreadPool.QueueUserWorkItem(AddressOf WorkMethod, autoEvent) ' Wait for foreground thread to end. regularThread.Join() ' Wait for background thread to end. autoEvent.WaitOne() End Sub Shared Sub ThreadMethod() Console.WriteLine("ThreadOne, executing ThreadMethod, " & _ "is from the thread pool? {0}", _ Thread.CurrentThread.IsThreadPoolThread) End Sub Shared Sub WorkMethod(stateInfo As Object) Console.WriteLine("ThreadTwo, executing WorkMethod, " & _ "is from the thread pool? {0}", _ Thread.CurrentThread.IsThreadPoolThread) ' Signal that this thread is finished. DirectCast(stateInfo, AutoResetEvent).Set() End Sub End Class
using System; using System.Threading; class IsThreadPool { static void Main() { AutoResetEvent autoEvent = new AutoResetEvent(false); Thread regularThread = new Thread(new ThreadStart(ThreadMethod)); regularThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoEvent); // Wait for foreground thread to end. regularThread.Join(); // Wait for background thread to end. autoEvent.WaitOne(); } static void ThreadMethod() { Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0}from the thread pool.", Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); } static void WorkMethod(object stateInfo) { Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0}from the thread pool.", Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); // Signal that this thread is finished. ((AutoResetEvent)stateInfo).Set(); } }
using namespace System; using namespace System::Threading; ref class IsThreadPool { public: static void ThreadMethod() { Console::WriteLine( "ThreadOne, executing ThreadMethod, " "is {0}from the thread pool.", Thread::CurrentThread->IsThreadPoolThread ? (String^)"" : "not " ); } static void WorkMethod( Object^ stateInfo ) { Console::WriteLine( "ThreadTwo, executing WorkMethod, " "is {0}from the thread pool.", Thread::CurrentThread->IsThreadPoolThread ? (String^)"" : "not " ); // Signal that this thread is finished. dynamic_cast<AutoResetEvent^>(stateInfo)->Set(); } }; int main() { AutoResetEvent^ autoEvent = gcnew AutoResetEvent( false ); Thread^ regularThread = gcnew Thread( gcnew ThreadStart( &IsThreadPool::ThreadMethod ) ); regularThread->Start(); ThreadPool::QueueUserWorkItem( gcnew WaitCallback( &IsThreadPool::WorkMethod ), autoEvent ); // Wait for foreground thread to end. regularThread->Join(); // Wait for background thread to end. autoEvent->WaitOne(); }
import System.*; import System.Threading.*; import System.Threading.Thread; class IsThreadPool { public static void main(String[] args) { AutoResetEvent autoEvent = new AutoResetEvent(false); Thread regularThread = new Thread(new ThreadStart(ThreadMethod)); regularThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoEvent); // Wait for foreground thread to end. regularThread.Join(); // Wait for background thread to end. autoEvent.WaitOne(); } //main static void ThreadMethod() { Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0}from the thread pool.", (Thread.get_CurrentThread().get_IsThreadPoolThread()) ? "" : "not "); } //ThreadMethod static void WorkMethod(Object stateInfo) { Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0}from the thread pool.", (Thread.get_CurrentThread().get_IsThreadPoolThread()) ? "" : "not "); // Signal that this thread is finished. ((AutoResetEvent)(stateInfo)).Set(); } //WorkMethod } //IsThreadPool

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Thread.Join メソッド (TimeSpan)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As Thread Dim timeout As TimeSpan Dim returnValue As Boolean returnValue = instance.Join(timeout)
戻り値
スレッドが終了した場合は true。timeout パラメータで指定した時間が経過してもスレッドが終了していない場合は false。


![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
timeout に Timeout.Infinite が指定されている場合、このメソッドは戻り値以外は Join メソッド オーバーロードと同じように動作します。
Join の呼び出し時にそのスレッドが既に終了している場合は、そのメソッドから制御がすぐに戻ります。
このメソッドは、現在のスレッドの状態を変更して、WaitSleepJoin の状態にします。ThreadState.Unstarted 状態のスレッドで Join を呼び出すことはできません。

Join メソッドで TimeSpan 値を使用する方法の例を次に示します。
Imports System Imports System.Threading Public Class Test Shared waitTime As New TimeSpan(0, 0, 1) <MTAThread> _ 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
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); } }
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." ); } }
import System.*; import System.Threading.*; import System.Threading.Thread; class Test { private static TimeSpan waitTime = new TimeSpan(0, 0, 1); public static void main(String[] args) { Thread newThread = new Thread(new ThreadStart(Work)); newThread.Start(); if (newThread.Join((waitTime.Add(waitTime)))) { Console.WriteLine("New thread terminated."); } else { Console.WriteLine("Join timed out."); } } //main static void Work() { Thread.Sleep(waitTime); } //Work } //Test

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Thread.Join メソッド (Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As Thread Dim millisecondsTimeout As Integer Dim returnValue As Boolean returnValue = instance.Join(millisecondsTimeout)
戻り値
スレッドが終了した場合は true。millisecondsTimeout パラメータで指定した時間が経過してもスレッドが終了していない場合は false。


![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
millisecondsTimeout パラメータに Timeout.Infinite が指定されている場合、このメソッドは戻り値以外は Join メソッド オーバーロードと同じように動作します。
Join の呼び出し時にそのスレッドが既に終了している場合は、そのメソッドから制御がすぐに戻ります。
このメソッドは、呼び出し元のスレッドの状態を変更して、ThreadState.WaitSleepJoin の状態が含まれるようにします。ThreadState.Unstarted 状態のスレッドで Join を呼び出すことはできません。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Thread.Join メソッド

名前 | 説明 |
---|---|
Thread.Join () | 1 つのスレッドが終了するまで呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。 .NET Compact Framework によってサポートされています。 |
Thread.Join (Int32) | 1 つのスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。 .NET Compact Framework によってサポートされています。 |
Thread.Join (TimeSpan) | 1 つのスレッドが終了するまで、または指定された時間が経過するまで、呼び出し元のスレッドをブロックします。標準 COM/SendMessage ポンピングの実行は継続されます。 |

Weblioに収録されているすべての辞書からThread.Joinを検索する場合は、下記のリンクをクリックしてください。

- Thread.Joinのページへのリンク