ParameterizedThreadStart 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示对 . 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# 编译器从 DoWork 和 DoMoreWork 方法的签名推断 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.'
注解
创建托管线程时,在线程上执行的方法由:
ThreadStart传递给构造函数的Thread.Thread(ThreadStart)委托。 任何没有参数且在 C# 中返回
void或Visual Basic中的Sub过程都可以表示委托的方法。ParameterizedThreadStart传递给构造函数的Thread.Thread(ParameterizedThreadStart)委托。 具有类型为 Object 且在 C# 中返回 void 或Visual Basic中的 Sub 过程的任何方法都可以表示委托。
在调用方法之前 Thread.Start ,线程不会开始执行。 在 ThreadStart 线程上调用或 ParameterizedThreadStart 委托,执行从委托表示的方法的第一行开始。 对于 ParameterizedThreadStart 委托,传递给 Start(Object) 方法的对象将传递给委托。
Note
Visual Basic和 C# 用户可以在创建线程时省略 ThreadStart 或 ParameterizedThreadStart 委托构造函数。 在 Visual Basic 中,将方法传递给 Thread 构造函数时,请使用 AddressOf 运算符;例如,Dim t As New Thread(AddressOf ThreadProc)。 在 C# 中,只需指定线程过程的名称。 编译器选择正确的委托构造函数。
Note
在 C++ 中为实例方法创建 ParameterizedThreadStart 委托时,构造函数的第一个参数是实例变量。 对于静态方法,构造函数的第一个参数为零。 对于静态方法,委托构造函数只需要一个参数:由类名限定的回调方法的地址。
委托 ParameterizedThreadStart 和 Thread.Start(Object) 方法重载可以轻松将数据传递到线程过程,但此方法不是类型安全的,因为任何对象都可以传递给 Thread.Start(Object)。 将数据传递到线程过程的更可靠方法是将线程过程和数据字段都放入工作器对象。 有关详细信息,请参阅 在开始时间创建线程和传递数据。
委托 ParameterizedThreadStart 仅支持单个参数。 可以通过将以下参数之一传递给 ParameterizedThreadStart 多个数据项:
- 数组。
- 集合类型,如果所有数据项都属于同一类型。
- 元组类型,例如 Tuple<T1,T2> 或 Tuple<T1,T2,T3,T4>。
扩展方法
| 名称 | 说明 |
|---|---|
| GetMethodInfo(Delegate) |
获取一个对象,该对象表示由指定委托表示的方法。 |