ParameterizedThreadStart 委托

定义

表示在 Thread 上执行的方法。Represents the method that executes on a Thread.

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

参数

obj
Object

包含线程过程的数据的对象。An object that contains data for the thread procedure.

继承
ParameterizedThreadStart
属性

示例

下面的代码示例使用ParameterizedThreadStart委托来执行静态方法和实例方法。The following code example uses a ParameterizedThreadStart delegate to execute a static method and an instance method. 第一个ParameterizedThreadStart委托由静态DoWork方法表示, 第二个委托由实例DoMoreWork方法表示。The first ParameterizedThreadStart delegate is represented by the static DoWork method and the second is represented by the instance DoMoreWork method. 这两个方法ParameterizedThreadStart都与委托签名匹配; 即, 它们具有一个类型Object为的参数, 不会返回值。Both methods match the ParameterizedThreadStart delegate signature; that is, they have a single parameter of type Object and don't return a value.

备注

C# Visual Basic 和编译器从ParameterizedThreadStart DoWorkDoMoreWork方法的签名推断委托, 并调用正确的构造函数。The Visual Basic and C# compilers infer the ParameterizedThreadStart delegate from the signatures of the DoWork and DoMoreWork methods, and call the correct constructor. 因此, 在代码中没有显式构造函数调用。Thus, there is no explicit constructor call in the code.

using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Work
    {
    public:
        void StartThreads()
        {
            // Start a thread that calls a parameterized static method.
            Thread^ newThread = gcnew
                Thread(gcnew ParameterizedThreadStart(Work::DoWork));
            newThread->Start(42);
              
            // Start a thread that calls a parameterized instance method.
            Work^ someWork = gcnew Work;
            newThread = gcnew Thread(
                        gcnew ParameterizedThreadStart(someWork,
                        &Work::DoMoreWork));
            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 example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
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.'

注解

创建托管线程时, 将通过以下方式表示在线程上执行的方法:When a managed thread is created, the method that executes on the thread is represented by:

在调用Thread.Start方法之前, 不会开始执行线程。The thread does not begin executing until the Thread.Start method is called. 在线程ParameterizedThreadStart上调用或委托,并在委托表示的方法的第一行开始执行。ThreadStartThe ThreadStart or ParameterizedThreadStart delegate is invoked on the thread, and execution begins at the first line of the method represented by the delegate. 对于ParameterizedThreadStart委托, 传递Start(Object)给方法的对象将传递给委托。In the case of the ParameterizedThreadStart delegate, the object that is passed to the Start(Object) method is passed to the delegate.

备注

创建线程C#时, Visual Basic 和ThreadStart用户ParameterizedThreadStart可以省略或委托构造函数。Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. 在 Visual Basic 中, 在AddressOf将方法传递Thread给构造函数时使用运算符; 例如, Dim t As New Thread(AddressOf ThreadProc)In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). 在C#中, 只需指定线程过程的名称。In C#, simply specify the name of the thread procedure. 编译器选择正确的委托构造函数。The compiler selects the correct delegate constructor.

备注

当您在中ParameterizedThreadStart C++创建实例方法的委托时, 该构造函数的第一个参数是实例变量。When you create a ParameterizedThreadStart delegate for an instance method in C++, the first parameter of the constructor is the instance variable. 对于静态方法, 该构造函数的第一个参数为零。For a static method, the first parameter of the constructor is zero. 对于静态方法, 委托构造函数只需要一个参数: 由类名限定的回调方法的地址。For a static method, the delegate constructor requires only one parameter: the address of the callback method, qualified by the class name.

委托和方法重载使您可以轻松地将数据传递给线程过程, 但此方法不是类型安全的, 因为任何Thread.Start(Object)对象均可传递给。 Thread.Start(Object) ParameterizedThreadStartThe ParameterizedThreadStart delegate and the Thread.Start(Object) method overload make it easy to pass data to a thread procedure, but this technique is not type safe because any object can be passed to Thread.Start(Object). 将数据传递给线程过程的一种更可靠的方法是将线程过程和数据字段放入辅助角色对象。A more robust way to pass data to a thread procedure is to put both the thread procedure and the data fields into a worker object. 有关详细信息, 请参阅在启动时创建线程和传递数据For more information, see Creating Threads and Passing Data at Start Time.

ParameterizedThreadStart委托仅支持一个参数。The ParameterizedThreadStart delegate supports only a single parameter. 可以通过将多个数据项的参数ParameterizedThreadStart传递给, 将其传递给:You can pass multiple data items to the ParameterizedThreadStart by making that parameter one of the following:

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅