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 コンストラクタのページへのリンク