Thread.Joinとは? わかりやすく解説

Thread.Join メソッド ()

1 つスレッド終了するまで呼び出し元のスレッドブロックします標準 COM/SendMessage ポンピング実行継続されます。

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ThreadStateException

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

ThreadInterruptedException

スレッド待機中に中断されます。

解説解説
メモメモ

このメソッド適用される 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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Thread.Join メソッド (TimeSpan)

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

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

timeout の値が負で、ミリ秒単位の Timeout.Infinite と等しくないか、または MaxValue ミリ秒より大きいです。

ThreadStateException

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

解説解説
メモメモ

このメソッド適用される HostProtectionAttribute 属性Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的にはアイコンダブルクリックコマンド入力、またはブラウザURL入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラストピックまたは「SQL Server プログラミングホスト保護属性」を参照してください

timeoutTimeout.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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Thread.Join メソッド (Int32)

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

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function Join ( _
    millisecondsTimeout As Integer _
) As Boolean
Dim instance As Thread
Dim millisecondsTimeout As Integer
Dim returnValue As Boolean

returnValue = instance.Join(millisecondsTimeout)
public bool Join (
    int millisecondsTimeout
)
public:
bool Join (
    int millisecondsTimeout
)
public boolean Join (
    int millisecondsTimeout
)
public function Join (
    millisecondsTimeout : int
) : boolean

パラメータ

millisecondsTimeout

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

戻り値
スレッド終了した場合truemillisecondsTimeout パラメータ指定した時間経過してスレッド終了してない場合false

例外例外
例外種類条件

ArgumentOutOfRangeException

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

ThreadStateException

スレッド起動されていません。

解説解説
メモメモ

このメソッド適用される HostProtectionAttribute 属性Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的にはアイコンダブルクリックコマンド入力、またはブラウザURL入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラストピックまたは「SQL Server プログラミングホスト保護属性」を参照してください

millisecondsTimeout パラメータTimeout.Infinite指定されている場合、このメソッド戻り値以外は Join メソッド オーバーロード同じよう動作します

Join呼び出し時にそのスレッドが既に終了している場合は、そのメソッドから制御がすぐに戻ります

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Thread.Join メソッド



このページでは「.NET Framework クラス ライブラリ リファレンス」からThread.Joinを検索した結果を表示しています。
Weblioに収録されているすべての辞書からThread.Joinを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からThread.Join を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Thread.Join」の関連用語

Thread.Joinのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Thread.Joinのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS