Thread クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.None)> _ Public NotInheritable Class Thread Inherits CriticalFinalizerObject Implements _Thread
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.None)] public sealed class Thread : CriticalFinalizerObject, _Thread
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::None)] public ref class Thread sealed : public CriticalFinalizerObject, _Thread

プロセスは、そのプロセスに関連付けられたプログラム コードの一部を実行する 1 つ以上のスレッドを作成できます。スレッドで実行するプログラム コードを指定するには、ThreadStart デリゲートまたは ParameterizedThreadStart デリゲートを使用します。ParameterizedThreadStart デリゲートを使用すると、スレッド プロシージャにデータを渡すことができます。
スレッドは、存在している間は常に、ThreadState で 1 つ以上定義した状態を保ちます。スレッドに対しては、ThreadPriority で定義したスケジューリング優先順位を要求できますが、その優先順位がオペレーティング システムで有効であるかどうかは保証されません。
GetHashCode を使用すると、マネージ スレッドの識別子の値を取得できます。スレッドの有効期間の間は、この値の取得元のアプリケーション ドメインに関係なく、他のスレッドの値と衝突することはありません。
![]() |
---|
オペレーティング システムの ThreadId は、マネージ スレッドとの固定的な関係を一切持ちません。アンマネージ ホストは、マネージ スレッドとアンマネージ スレッドの関係を制御できるためです。たとえば、高度なホストでは、CLR Hosting API を使用して、同一のオペレーティング システム スレッドに対して複数のマネージ スレッドをスケジュールしたり、1 つのマネージ スレッドを複数のオペレーティング システム スレッド間で移動させたりすることができます。 |

Imports System Imports System.Threading ' Simple threading scenario: Start a Shared method running ' on a second thread. Public Class ThreadExample ' The ThreadProc method is called when the thread starts. ' It loops ten times, writing to the console and yielding ' the rest of its time slice each time, and then ends. Public Shared Sub ThreadProc() Dim i As Integer For i = 0 To 9 Console.WriteLine("ThreadProc: {0}", i) ' Yield the rest of the time slice. Thread.Sleep(0) Next End Sub Public Shared Sub Main() Console.WriteLine("Main thread: Start a second thread.") ' The constructor for the Thread class requires a ThreadStart ' delegate. The Visual Basic AddressOf operator creates this ' delegate for you. Dim t As New Thread(AddressOf ThreadProc) ' Start ThreadProc. On a uniprocessor, the thread does not get ' any processor time until the main thread yields. Uncomment ' the Thread.Sleep that follows t.Start() to see the difference. t.Start() 'Thread.Sleep(0) Dim i As Integer For i = 1 To 4 Console.WriteLine("Main thread: Do some work.") Thread.Sleep(0) Next Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.") t.Join() Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.") Console.ReadLine() End Sub End Class
using System; using System.Threading; // Simple threading scenario: Start a static method running // on a second thread. public class ThreadExample { // The ThreadProc method is called when the thread starts. // It loops ten times, writing to the console and yielding // the rest of its time slice each time, and then ends. public static void ThreadProc() { for (int i = 0; i < 10; i++) { Console.WriteLine("ThreadProc: {0}", i); // Yield the rest of the time slice. Thread.Sleep(0); } } public static void Main() { Console.WriteLine("Main thread: Start a second thread."); // The constructor for the Thread class requires a ThreadStart // delegate that represents the method to be executed on the // thread. C# simplifies the creation of this delegate. Thread t = new Thread(new ThreadStart(ThreadProc)); // Start ThreadProc. On a uniprocessor, the thread does not get // any processor time until the main thread yields. Uncomment // the Thread.Sleep that follows t.Start() to see the difference. t.Start(); //Thread.Sleep(0); for (int i = 0; i < 4; i++) { Console.WriteLine("Main thread: Do some work."); Thread.Sleep(0); } Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends."); t.Join(); Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program."); Console.ReadLine(); } }
// [C++] // Compile using /clr option. using namespace System; using namespace System::Threading; // Simple threading scenario: Start a Shared method running // on a second thread. public ref class ThreadExample { public: // The ThreadProc method is called when the thread starts. // It loops ten times, writing to the console and yielding // the rest of its time slice each time, and then ends. static void ThreadProc() { for ( int i = 0; i < 10; i++ ) { Console::Write( "ThreadProc: " ); Console::WriteLine( i ); // Yield the rest of the time slice. Thread::Sleep( 0 ); } } }; int main() { Console::WriteLine( "Main thread: Start a second thread." ); // Create the thread, passing a ThreadStart delegate that // represents the ThreadExample::ThreadProc method. For a // delegate representing a static method, no object is // required. Thread^ oThread = gcnew Thread( gcnew ThreadStart( &ThreadExample::ThreadProc ) ); // Start the thread. On a uniprocessor, the thread does not get // any processor time until the main thread yields. Uncomment // the Thread.Sleep that follows t.Start() to see the difference. oThread->Start(); //Thread::Sleep(0); for ( int i = 0; i < 4; i++ ) { Console::WriteLine( "Main thread: Do some work." ); Thread::Sleep( 0 ); } Console::WriteLine( "Main thread: Call Join(), to wait until ThreadProc ends." ); oThread->Join(); Console::WriteLine( "Main thread: ThreadProc.Join has returned. Press Enter to end program." ); Console::ReadLine(); return 0; }
import System.*; import System.Threading.*; // Simple threading scenario: Start a static method running // on a second thread. public class ThreadExample { // The ThreadProc method is called when the thread starts. // It loops ten times, writing to the console and yielding // the rest of its time slice each time, and then ends. public static void ThreadProc() throws InterruptedException { for (int i = 0; i < 10; i++) { Console.WriteLine("ThreadProc: {0}", System.Convert.ToString(i)); // Yield the rest of the time slice. Thread.sleep(0); } } //ThreadProc public static void main(String[] args) throws InterruptedException { Console.WriteLine("Main thread: Start a second thread."); // The constructor for the Thread class requires a ThreadStart // delegate that represents the method to be executed on the // thread. J# simplifies the creation of this delegate. System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(ThreadProc)); // Start ThreadProc. On a uniprocessor, the thread does not get // any processor time until the main thread yields. Uncomment // the Thread.Sleep that follows t.Start() to see the difference. t.Start(); //Thread.Sleep(0); for (int i = 0; i < 4; i++) { Console.WriteLine("Main thread: Do some work."); Thread.sleep(0); } Console.WriteLine("Main thread: Call Join(), to wait until " + "ThreadProc ends."); t.Join(); Console.WriteLine("Main thread: ThreadProc.Join has returned." + " Press Enter to end program."); Console.ReadLine(); } //main } //ThreadExample
[VB, C++, C#] Main thread: Start a second thread. Main thread: Do some work. ThreadProc: 0 Main thread: Do some work. ThreadProc: 1 Main thread: Do some work. ThreadProc: 2 Main thread: Do some work. ThreadProc: 3 Main thread: Call Join(), to wait until ThreadProc ends. ThreadProc: 4 ThreadProc: 5 ThreadProc: 6 ThreadProc: 7 ThreadProc: 8 ThreadProc: 9 Main thread: ThreadProc.Join has returned. Press Enter to end program.

System.Runtime.ConstrainedExecution.CriticalFinalizerObject
System.Threading.Thread


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


Thread コンストラクタ (ParameterizedThreadStart)
アセンブリ: mscorlib (mscorlib.dll 内)



スレッドの作成時には、スレッドは実行を開始しません。スレッドの実行をスケジュールするには、Start メソッドを呼び出します。データ オブジェクトをスレッドに渡すには、Start(Object) メソッド オーバーロードを使用します。
![]() |
---|
Visual Basic の場合は、スレッドを作成するときに ThreadStart コンストラクタを省略できます。メソッドを渡すときは、Dim t As New Thread(AddressOf ThreadProc) のように、AddressOf 演算子を使用します。Visual Basic では ThreadStart コンストラクタが自動的に呼び出されます。 |

静的メソッドおよびインスタンス メソッドで ParameterizedThreadStart デリゲートを作成および使用するための構文のコード例を次に示します。
Imports System Imports System.Threading Public Class Work <MTAThread> _ Shared Sub Main() ' To start a thread using a shared thread procedure, use ' the class name and method name when you create the ' ParameterizedThreadStart delegate. Visual Basic expands ' the AddressOf expression to the appropriate delegate ' creation syntax: ' New ParameterizedThreadStart(AddressOf Work.DoWork) ' Dim newThread As New Thread(AddressOf Work.DoWork) ' Use the overload of the Start method that has a ' parameter of type Object. You can create an object that ' contains several pieces of data, or you can pass any ' object or value type. The following code passes the ' integer value 42. ' newThread.Start(42) ' To start a thread using an instance method for the thread ' procedure, use the instance variable and method name when ' you create the ParameterizedThreadStart delegate. Visual ' Basic expands the AddressOf expression to the appropriate ' delegate creation syntax: ' New ParameterizedThreadStart(AddressOf w.DoMoreWork) ' Dim w As New Work() newThread = New Thread(New ParameterizedThreadStart(AddressOf w.DoMoreWork)) 'newThread = New Thread(AddressOf w.DoMoreWork) ' Pass an object containing data for the thread. ' newThread.Start("The answer.") End Sub Public Shared Sub DoWork(ByVal data As Object) Console.WriteLine("Static thread procedure. Data='{0}'", _ data) End Sub Public Sub DoMoreWork(ByVal data As Object) Console.WriteLine("Instance thread procedure. Data='{0}'", _ data) End Sub End Class ' This code example produces the following output (the order ' of the lines might vary): ' 'Static thread procedure. Data='42' 'Instance thread procedure. Data='The answer'
using System; using System.Threading; public class Work { public static void Main() { // To start a thread using a shared thread procedure, use // the class name and method name when you create the // ParameterizedThreadStart delegate. // Thread newThread = new Thread( new ParameterizedThreadStart(Work.DoWork)); // Use the overload of the Start method that has a // parameter of type Object. You can create an object that // contains several pieces of data, or you can pass any // reference type or value type. The following code passes // the integer value 42. // newThread.Start(42); // To start a thread using an instance method for the thread // procedure, use the instance variable and method name when // you create the ParameterizedThreadStart delegate. // Work w = new Work(); newThread = new Thread( new ParameterizedThreadStart(w.DoMoreWork)); // Pass an object containing data for the thread. // newThread.Start("The answer."); } public static void DoWork(object data) { Console.WriteLine("Static thread procedure. Data='{0}'", data); } public void DoMoreWork(object data) { Console.WriteLine("Instance thread procedure. Data='{0}'", data); } } /* This code example produces the following output (the order of the lines might vary): Static thread procedure. Data='42' Instance thread procedure. Data='The answer' */
using namespace System; using namespace System::Threading; namespace SystemThreadingExample { public ref class Work { public: void StartThreads() { // To start a thread using a shared thread procedure, use // the class name and method name when you create the // ParameterizedThreadStart delegate. // AddressOf Work.DoWork) // Thread^ newThread = gcnew Thread(gcnew ParameterizedThreadStart(Work::DoWork)); // Use the overload of the Start method that has a // parameter of type Object. You can create an object that // contains several pieces of data, or you can pass any // reference type or value type. The following code passes // the integer value 42. newThread->Start(42); // To start a thread using an instance method for the thread // procedure, use the instance variable and method name when // you create the ParameterizedThreadStart delegate. Work^ someWork = gcnew Work; newThread = gcnew Thread( gcnew ParameterizedThreadStart(someWork, &Work::DoMoreWork)); // Pass an object containing data for the thread. // newThread->Start("The answer."); } static void DoWork(Object^ data) { Console::WriteLine("Static thread procedure. Data='{0}'", data); } void DoMoreWork(Object^ data) { Console::WriteLine("Instance thread procedure. Data='{0}'", data); } }; } //Entry point of example application int main() { SystemThreadingExample::Work^ samplework = gcnew SystemThreadingExample::Work(); samplework->StartThreads(); } // This code example produces the following output (the order // of the lines might vary): // Static thread procedure. Data='42' // Instance thread procedure. Data='The answer'

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 コンストラクタ (ThreadStart, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim start As ThreadStart Dim maxStackSize As Integer Dim instance As New Thread(start, maxStackSize)


このコンストラクタのオーバーロードを使用しないでください。スレッドのスタック サイズとして推奨されるのは、Thread(ThreadStart) コンストラクタのオーバーロードで使用される既定のスタック サイズです。スレッドにメモリの問題がある場合、無限再帰のようなプログラミング エラーが原因と考えられます。
スタック サイズを非常に小さく指定した場合、スタック オーバーフロー調査を無効にする必要が生じることがあります。スタックの制約を厳しくすると、この調査自体がスタック オーバーフローの原因になることがあります。スタック オーバーフロー調査を無効にするには、アプリケーション設定ファイルに次のコードを追加します。
<configuration> <runtime> <disableStackOverflowProbing enabled="true"/> </runtime> </configuration>

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 コンストラクタ (ParameterizedThreadStart, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim start As ParameterizedThreadStart Dim maxStackSize As Integer Dim instance As New Thread(start, maxStackSize)


このコンストラクタのオーバーロードを使用しないでください。スレッドのスタック サイズとして推奨されるのは、Thread(ParameterizedThreadStart) コンストラクタのオーバーロードで使用される既定のスタック サイズです。スレッドにメモリの問題がある場合、無限再帰のようなプログラミング エラーが原因と考えられます。
スタック サイズを非常に小さく指定した場合、スタック オーバーフロー調査を無効にする必要が生じることがあります。スタックの制約を厳しくすると、この調査自体がスタック オーバーフローの原因になることがあります。スタック オーバーフロー調査を無効にするには、アプリケーション設定ファイルに次のコードを追加します。
<configuration> <runtime> <disableStackOverflowProbing enabled="true"/> </runtime> </configuration>

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 コンストラクタ

名前 | 説明 |
---|---|
Thread (ParameterizedThreadStart) | スレッドの開始時にオブジェクトをスレッドに渡すことを許可するデリゲートを指定して、Thread クラスの新しいインスタンスを初期化します。 |
Thread (ThreadStart) | Thread クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Thread (ParameterizedThreadStart, Int32) | Thread クラスの新しいインスタンスを初期化して、スレッドの開始時にオブジェクトをスレッドに渡すことを許可するデリゲートとこのスレッドの最大スタック サイズを指定します。 |
Thread (ThreadStart, Int32) | Thread クラスの新しいインスタンスを初期化して、スレッドの最大スタック サイズを指定します。 .NET Compact Framework によってサポートされています。 |

Thread コンストラクタ (ThreadStart)
アセンブリ: mscorlib (mscorlib.dll 内)



スレッドの作成時には、スレッドは実行を開始しません。スレッドの実行をスケジュールするには、Start メソッドを呼び出します。
![]() |
---|
Visual Basic の場合は、スレッドを作成するときに ThreadStart コンストラクタを省略できます。メソッドを渡すときは、Dim t As New Thread(AddressOf ThreadProc) のように AddressOf 演算子を使用します。Visual Basic では ThreadStart コンストラクタが自動的に呼び出されます。 |

静的メソッドを実行するスレッドを作成する方法を次のコード例に示します。
Imports System Imports System.Threading Public Class Test <MTAThread> _ Shared Sub Main() Dim newThread As New Thread(AddressOf Work.DoWork) newThread.Start() End Sub End Class Public Class Work Private Sub New() End Sub Shared Sub DoWork() End Sub End Class
using System; using System.Threading; class Test { static void Main() { Thread newThread = new Thread(new ThreadStart(Work.DoWork)); newThread.Start(); } } class Work { Work() {} public static void DoWork() {} }
using namespace System; using namespace System::Threading; ref class Work { private: Work(){} public: static void DoWork(){} }; int main() { Thread^ newThread = gcnew Thread( gcnew ThreadStart( &Work::DoWork ) ); newThread->Start(); }
import System.*; import System.Threading.*; import System.Threading.Thread; class Test { public static void main(String[] args) { Thread newThread = new Thread(new ThreadStart(Work.DoWork)); newThread.Start(); } //main } //Test class Work { Work() { } //Work public static void DoWork() { } //DoWork } //Work
インスタンス メソッドを実行するスレッドを作成する方法を次のコード例に示します。
Imports System Imports System.Threading Public Class Test <MTAThread> _ Shared Sub Main() Dim threadWork As New Work() Dim newThread As New Thread(AddressOf threadWork.DoWork) newThread.Start() End Sub End Class Public Class Work Sub New() End Sub Sub DoWork() End Sub End Class

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 プロパティ

名前 | 説明 | |
---|---|---|
![]() | CurrentUICulture | 実行時にカルチャ固有のリソースを検索するためにリソース マネージャで使用される、現在のカルチャを取得または設定します。 |
![]() | ExecutionContext | 現在のスレッドのさまざまなコンテキストに関する情報を格納する ExecutionContext オブジェクトを取得します。 |
![]() | IsAlive | 現在のスレッドの実行ステータスを示す値を取得します。 |
![]() ![]() | IsThreadPoolThread | スレッドがマネージ スレッド プールに所属しているかどうかを示す値を取得します。 |
![]() ![]() ![]() ![]() | ThreadState | 現在のスレッドの状態を示す値を取得します。 |

Thread メソッド


名前 | 説明 | |
---|---|---|
![]() | System.Runtime.InteropServices._Thread.GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 |
![]() | System.Runtime.InteropServices._Thread.GetTypeInfo | オブジェクトの型情報を取得します。その後は、インターフェイスの型情報の取得に使用できます。 |
![]() | System.Runtime.InteropServices._Thread.GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 |
![]() | System.Runtime.InteropServices._Thread.Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |

Thread メンバ
スレッドを作成および制御し、そのスレッドの優先順位の設定およびステータスの取得を行います。
Thread データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | CurrentUICulture | 実行時にカルチャ固有のリソースを検索するためにリソース マネージャで使用される、現在のカルチャを取得または設定します。 |
![]() | ExecutionContext | 現在のスレッドのさまざまなコンテキストに関する情報を格納する ExecutionContext オブジェクトを取得します。 |
![]() | IsAlive | 現在のスレッドの実行ステータスを示す値を取得します。 |
![]() ![]() | IsThreadPoolThread | スレッドがマネージ スレッド プールに所属しているかどうかを示す値を取得します。 |
![]() ![]() ![]() ![]() | ThreadState | 現在のスレッドの状態を示す値を取得します。 |


名前 | 説明 | |
---|---|---|
![]() | System.Runtime.InteropServices._Thread.GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 |
![]() | System.Runtime.InteropServices._Thread.GetTypeInfo | オブジェクトの型情報を取得します。その後は、インターフェイスの型情報の取得に使用できます。 |
![]() | System.Runtime.InteropServices._Thread.GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 |
![]() | System.Runtime.InteropServices._Thread.Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |

_Thread インターフェイス
アセンブリ: mscorlib (mscorlib.dll 内)

<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _ <ComVisibleAttribute(True)> _ <GuidAttribute("C281C7F1-4AA9-3517-961A-463CFED57E75")> _ <CLSCompliantAttribute(False)> _ Public Interface _Thread
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] [ComVisibleAttribute(true)] [GuidAttribute("C281C7F1-4AA9-3517-961A-463CFED57E75")] [CLSCompliantAttribute(false)] public interface _Thread
[InterfaceTypeAttribute(ComInterfaceType::InterfaceIsIUnknown)] [ComVisibleAttribute(true)] [GuidAttribute(L"C281C7F1-4AA9-3517-961A-463CFED57E75")] [CLSCompliantAttribute(false)] public interface class _Thread


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 メソッド

名前 | 説明 | |
---|---|---|
![]() | GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 |
![]() | GetTypeInfo | オブジェクトの型情報を取得します。その後は、インターフェイスの型情報の取得に使用できます。 |
![]() | GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 |
![]() | Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |

_Thread メンバ
System.Threading.Thread クラスをアンマネージ コードに公開します。
_Thread データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 |
![]() | GetTypeInfo | オブジェクトの型情報を取得します。その後は、インターフェイスの型情報の取得に使用できます。 |
![]() | GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 |
![]() | Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |

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

- Threadのページへのリンク