共用方式為


ThreadStart 代理人

定義

代表在 Thread 上執行的方法。

public delegate void ThreadStart();
public delegate void ThreadStart();
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ThreadStart();
type ThreadStart = delegate of unit -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadStart = delegate of unit -> unit
Public Delegate Sub ThreadStart()
屬性

範例

下列程式碼範例示範如何建立和使用 ThreadStart 委派搭配實例方法和靜態方法的語法。

如需示範如何建立委派的另一個 ThreadStart 簡單範例,請參閱 Thread.Start() 方法多載。 如需執行緒建立的詳細資訊,請參閱 在開始時間建立執行緒和傳遞資料

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
 */
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
 */
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

備註

建立 Managed 執行緒時,線上程上執行的方法是由委派或 ParameterizedThreadStart 傳遞至建構函式的 Thread 委派表示 ThreadStart 。 在呼叫 方法之前 Thread.Start ,執行緒不會開始執行。 執行會從 或 ParameterizedThreadStart 委派所 ThreadStart 表示之方法的第一行開始。

注意

Visual Basic和 C# 使用者可以在建立執行緒時省略 ThreadStartParameterizedThreadStart 委派建構函式。 在 Visual Basic中,將方法傳遞至 Thread 建構函式時,請使用 AddressOf 運算子;例如 。 Dim t As New Thread(AddressOf ThreadProc) 在 C# 中,只要指定執行緒程式的名稱即可。 編譯器會選取正確的委派建構函式。

針對 C++,從 .NET Framework 2.0 開始,建立靜態方法的委派只需要一個 ThreadStart 參數:由類別名稱限定的回呼方法位址。 在舊版中,建立靜態方法的委派時需要兩個參數:零 (null) 和方法位址。 針對實例方法,所有版本都需要兩個參數:執行個體變數和方法位址。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱