ParameterizedThreadStart 代理人

定義

代表在 上 Thread執行的方法。

public delegate void ParameterizedThreadStart(System::Object ^ obj);
[System.Runtime.InteropServices.ComVisible(false)]
public delegate void ParameterizedThreadStart(object obj);
public delegate void ParameterizedThreadStart(object obj);
[<System.Runtime.InteropServices.ComVisible(false)>]
type ParameterizedThreadStart = delegate of obj -> unit
type ParameterizedThreadStart = delegate of obj -> unit
Public Delegate Sub ParameterizedThreadStart(obj As Object)

參數

obj
Object

一個包含執行緒程序資料的物件。

屬性

範例

以下程式碼範例使用 ParameterizedThreadStart 代理來執行靜態方法與實例方法。 第一個 ParameterizedThreadStart 代理以靜態 DoWork 方法表示,第二個代表則以實例 DoMoreWork 方法表示。 這兩種方法都符合 ParameterizedThreadStart 代理簽名;也就是說,它們只有一個型別 Object 的參數,且不會回傳任何值。

Note

Visual Basic 和 C# 編譯器會從 DoWorkDoMoreWork 方法的簽名推導出 ParameterizedThreadStart 代理,並呼叫正確的建構子。 因此,程式碼中沒有明確的建構子呼叫。

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // Start a thread that calls a parameterized static method.
        Thread newThread = new Thread(Work.DoWork);
        newThread.Start(42);

        // Start a thread that calls a parameterized instance method.
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        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 example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
Imports System.Threading

Public Class Work
    Shared Sub Main()
        ' Start a thread that calls a parameterized static method.
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread.Start(42)

        ' Start a thread that calls a parameterized instance method.
        Dim w As New Work()
        newThread = New Thread(AddressOf w.DoMoreWork)
        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 example displays output like the following:
'    Static thread procedure. Data='42'
'    Instance thread procedure. Data='The answer.'

備註

當建立受管理執行緒時,執行該執行緒的方法以以下方式表示:

執行緒直到 Thread.Start 方法被呼叫前才開始執行。 ThreadStartParameterizedThreadStart代理在執行緒中被呼叫,執行從代理所代表方法的第一行開始。 在代理 ParameterizedThreadStart 的情況下,傳遞給 Start(Object) 方法的物件會傳給代理。

Note

Visual Basic 和 C# 使用者在建立執行緒時可以省略 ThreadStartParameterizedThreadStart 的代理建構子。 在Visual Basic中,將方法傳給 Thread 建構子時,使用 AddressOf 運算子;例如,Dim t As New Thread(AddressOf ThreadProc)。 在 C# 中,只需指定執行緒程序的名稱即可。 編譯器會選擇正確的代理建構器。

Note

當你在 C++ 中為實例方法建立 ParameterizedThreadStart 代理時,建構子的第一個參數是實例變數。 對於靜態方法,建構子的第一個參數為零。 對於靜態方法,代理建構子只需一個參數:回調方法的位址,並以類別名稱作為限定。

ParameterizedThreadStart代理與Thread.Start(Object)方法過載使得將資料傳遞給執行緒程序變得容易,但此技術並非型別安全,因為任何物件都可以傳遞到 Thread.Start(Object)。 更穩健的方式是將執行緒程序與資料欄位同時放入工作者物件中。 更多資訊請參閱 「建立執行緒及開始時傳遞資料」。

ParameterizedThreadStart代表只支援單一參數。 你可以將多個資料項目傳給 , ParameterizedThreadStart 方法是將該參數設為以下之一:

擴充方法

名稱 Description
GetMethodInfo(Delegate)

取得一個代表指定代理所代表方法的物件。

適用於

另請參閱