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



![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
スレッドが ThreadState.Running 状態になると、オペレーティング システムによってスレッドの実行がスケジュールされます。スレッドは、スレッド コンストラクタに提供された ThreadStart デリゲートまたは ParameterizedThreadStart デリゲートによって表されるメソッドの最初の行の実行を開始します。

この出力ステートメントのシーケンスは一般的ですが、どのシステムでも同じであることは保証されません。
スレッド プロシージャは、静的メソッドまたはインスタンス メソッドのいずれかです。ThreadStart デリゲートに関するコード例を参照してください。スレッド作成の詳細については、「スレッドを作成し、開始時にデータを渡す」を参照してください。
Imports System Imports System.Threading Public Class ThreadWork Public Shared Sub DoWork() Dim i As Integer For i = 0 To 2 Console.WriteLine("Working thread...") Thread.Sleep(100) Next i End Sub 'DoWork End Class 'ThreadWork Class ThreadTest Public Shared Sub Main() Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork) Dim myThread As New Thread(myThreadDelegate) myThread.Start() Dim i As Integer For i = 0 To 2 Console.WriteLine("In main.") Thread.Sleep(100) Next i End Sub 'Main End Class 'ThreadTest
using System; using System.Threading; public class ThreadWork { public static void DoWork() { for(int i = 0; i<3;i++) { Console.WriteLine("Working thread..."); Thread.Sleep(100); } } } class ThreadTest { public static void Main() { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); myThread.Start(); for(int i = 0; i<3; i++) { Console.WriteLine("In main."); Thread.Sleep(100); } } }
using namespace System; using namespace System::Threading; public ref class ThreadWork { public: static void DoWork() { for ( int i = 0; i < 3; i++ ) { Console::WriteLine( "Working thread..." ); Thread::Sleep( 100 ); } } }; int main() { ThreadStart^ myThreadDelegate = gcnew ThreadStart( &ThreadWork::DoWork ); Thread^ myThread = gcnew Thread( myThreadDelegate ); myThread->Start(); for ( int i = 0; i < 3; i++ ) { Console::WriteLine( "In main." ); Thread::Sleep( 100 ); } }
import System.*; import System.Threading.*; public class ThreadWork { public static void DoWork() { for (int i = 0; i < 3; i++) { Console.WriteLine("Working thread..."); System.Threading.Thread.Sleep(100); } } //DoWork } //ThreadWork class ThreadTest { public static void main(String[] args) { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); System.Threading.Thread myThread = new System.Threading.Thread(myThreadDelegate); myThread.Start(); for (int i = 0; i < 3; i++) { Console.WriteLine("In main."); System.Threading.Thread.Sleep(100); } } //main } //ThreadTest

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.Start メソッド (Object)
アセンブリ: mscorlib (mscorlib.dll 内)



![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
スレッドが ThreadState.Running 状態になると、オペレーティング システムによってスレッドの実行がスケジュールされます。スレッドは、スレッド コンストラクタに提供された ThreadStart デリゲートまたは ParameterizedThreadStart デリゲートによって表されるメソッドの最初の行の実行を開始します。
いったんスレッドを終了すると、Start のための別の呼び出しを使っても再起動することはできません。
このオーバーロードと ParameterizedThreadStart デリゲートを使用すると、データをスレッド プロシージャに簡単に渡せるようになります。ただし、この方法では、どのオブジェクトでもこのオーバーロードに渡すことができるため、タイプ セーフではありません。より信頼性の高い方法でスレッド プロシージャにデータを渡すには、スレッド プロシージャとデータ フィールドの両方をワーカー オブジェクトに格納します。詳細については、「スレッドを作成し、開始時にデータを渡す」を参照してください。

静的メソッドおよびインスタンス メソッドで 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.Start メソッド

名前 | 説明 |
---|---|
Thread.Start () | オペレーティング システムによって、現在のインスタンスの状態を ThreadState.Running に変更します。 .NET Compact Framework によってサポートされています。 |
Thread.Start (Object) | オペレーティング システムによって現在のインスタンスの状態が ThreadState.Running に変更され、オプションでスレッドが実行するメソッドで使用するデータを格納するオブジェクトが提供されます。 |

ThreadStart デリゲート
アセンブリ: mscorlib (mscorlib.dll 内)


マネージ スレッドを作成すると、そのスレッドで実行するメソッドが、Thread コンストラクタに渡される ThreadStart デリゲートまたは ParameterizedThreadStart デリゲートで表されます。スレッドは System.Threading.Thread.Start メソッドが呼び出されるまで実行を開始しません。実行は、ThreadStart または ParameterizedThreadStart デリゲートで表されるメソッドの最初の行から開始されます。
![]() |
---|
Visual Basic および C# のユーザーは、スレッドを作成するときに ThreadStart または ParameterizedThreadStart デリゲート コンストラクタを省略できます。Visual Basic では、メソッドを Thread コンストラクタに渡すときに、Dim t As New Thread(AddressOf ThreadProc) のように AddressOf 演算子を使用します。C# では、単純にスレッド プロシージャの名前を指定します。コンパイラにより、適切なデリゲート コンストラクタが選択されます。 |

インスタンス メソッドおよび静的メソッドで ThreadStart デリゲートを作成および使用するための構文のコード例を次に示します。
ThreadStart デリゲートを作成する方法の別の単純な例については、Thread.Start メソッドのオーバーロードに関するトピックを参照してください。スレッド作成の詳細については、「スレッドを作成し、開始時にデータを渡す」を参照してください。
Imports System Imports System.Threading Public Class Test <MTAThread> _ Shared Sub Main() ' To start a thread using a static thread procedure, use the ' class name and method name when you create the ThreadStart ' delegate. Visual Basic expands the AddressOf expression ' to the appropriate delegate creation syntax: ' New ThreadStart(AddressOf Work.DoWork) ' Dim newThread As New Thread(AddressOf Work.DoWork) newThread.Start() ' To start a thread using an instance method for the thread ' procedure, use the instance variable and method name when ' you create the ThreadStart delegate. Visual Basic expands ' the AddressOf expression to the appropriate delegate ' creation syntax: ' New ThreadStart(AddressOf w.DoMoreWork) ' Dim w As New Work() w.Data = 42 newThread = new Thread(AddressOf w.DoMoreWork) newThread.Start() End Sub End Class Public Class Work Public Shared Sub DoWork() Console.WriteLine("Static thread procedure.") End Sub Public Data As Integer Public Sub DoMoreWork() 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. 'Instance thread procedure. Data=42
using System; using System.Threading; class Test { static void Main() { // To start a thread using a static thread procedure, use the // class name and method name when you create the ThreadStart // delegate. Beginning in version 2.0 of the .NET Framework , // it is not necessary to create a delegate explicityly. // Specify the name of the method in the Thread constructor, // and the compiler selects the correct delegate. For example: // // Thread newThread = new Thread(Work.DoWork); // ThreadStart threadDelegate = new ThreadStart(Work.DoWork); Thread newThread = new Thread(threadDelegate); newThread.Start(); // To start a thread using an instance method for the thread // procedure, use the instance variable and method name when // you create the ThreadStart delegate. Beginning in version // 2.0 of the .NET Framework, the explicit delegate is not // required. // Work w = new Work(); w.Data = 42; threadDelegate = new ThreadStart(w.DoMoreWork); newThread = new Thread(threadDelegate); newThread.Start(); } } class Work { public static void DoWork() { Console.WriteLine("Static thread procedure."); } public int Data; public void DoMoreWork() { 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. Instance thread procedure. Data=42 */
using namespace System; using namespace System::Threading; ref class Work { public: static void DoWork() { Console::WriteLine( "Static thread procedure." ); } int Data; void DoMoreWork() { Console::WriteLine( "Instance thread procedure. Data={0}", Data ); } }; int main() { // To start a thread using an instance method for the thread // procedure, specify the object as the first argument of the // ThreadStart constructor. // Work^ w = gcnew Work; w->Data = 42; ThreadStart^ threadDelegate = gcnew ThreadStart( w, &Work::DoMoreWork ); Thread^ newThread = gcnew Thread( threadDelegate ); newThread->Start(); // To start a thread using a static thread procedure, specify // only the address of the procedure. This is a change from // earlier versions of the .NET Framework, which required // two arguments, the first of which was null (0). // threadDelegate = gcnew ThreadStart( &Work::DoWork ); newThread = gcnew Thread( threadDelegate ); newThread->Start(); } /* This code example produces the following output (the order of the lines might vary): Static thread procedure. Instance thread procedure. Data=42 */
import System.*; import System.Threading.*; import System.Threading.Thread; class Test { public static void main(String[] args) { ThreadStart threadDelegate = new ThreadStart(Work.DoWork); Thread newThread = new Thread(threadDelegate); newThread.Start(); } //main } //Test class Work { public static void DoWork() { } //DoWork } //Work

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.Startのページへのリンク