英語で読む

次の方法で共有


ThreadStart 代理人

定義

Thread で実行するメソッドを表します。

C#
public delegate void ThreadStart();
C#
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ThreadStart();
属性

次のコード例は、インスタンス メソッドと静的メソッドを使用してデリゲートを ThreadStart 作成および使用するための構文を示しています。

デリゲートを作成する方法を示すもう 1 つの簡単な ThreadStart 例については、 メソッドのオーバーロードに関するページを Thread.Start() 参照してください。 スレッドの作成の詳細については、「 スレッドの作成と開始時のデータの受け渡し」を参照してください。

C#
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 explicitly. 
        // 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
 */

注釈

マネージド スレッドが作成されると、スレッドで実行されるメソッドは、コンストラクターに渡されるデリゲートまたはデリゲートによってThreadStart表されますThreadParameterizedThreadStart メソッドが呼び出されるまで、スレッドの Thread.Start 実行は開始されません。 実行は、 または ParameterizedThreadStart デリゲートによって表されるメソッドの最初の行からThreadStart開始されます。

注意

Visual Basic と C# のユーザーは、スレッドの作成時に ThreadStart または ParameterizedThreadStart デリゲート コンストラクターを省略できます。 Visual Basic では、 メソッドを AddressOf コンストラクターに渡すときに 演算子を Thread 使用します (例: Dim t As New Thread(AddressOf ThreadProc))。 C# では、スレッド プロシージャの名前を指定するだけです。 コンパイラは、適切なデリゲート コンストラクターを選択します。

C++ の場合、.NET Framework 2.0 以降では、静的メソッドのデリゲートを作成するときにThreadStart必要なパラメーターは、コールバック メソッドのアドレス (クラス名で修飾) の 1 つだけです。 以前のバージョンでは、静的メソッドのデリゲートを作成するときに、0 (null) とメソッド アドレスの 2 つのパラメーターが必要でした。 インスタンス メソッドの場合、すべてのバージョンには、インスタンス変数とメソッド アドレスの 2 つのパラメーターが必要です。

拡張メソッド

GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください