ThreadStart 委托

定义

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

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

示例

下面的代码示例演示用于创建ThreadStart委托并将委托与实例方法和静态方法一起使用的语法。The following code example shows the syntax for creating and using a ThreadStart delegate with an instance method and with a static method.

有关演示如何创建ThreadStart委托的另一个简单示例, Thread.Start()请参阅方法重载。For another simple example that demonstrates how to create a ThreadStart delegate, see the Thread.Start() method overload. 有关创建线程的详细信息, 请参阅在启动时创建线程和传递数据For more information about thread creation, see Creating Threads and Passing Data at Start Time.

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

注解

创建托管线程时, 在该线程上执行的方法由传递给ThreadStart Thread构造函数的委托ParameterizedThreadStart或委托表示。When a managed thread is created, the method that executes on the thread is represented by a ThreadStart delegate or a ParameterizedThreadStart delegate that is passed to the Thread constructor. 在调用Thread.Start方法之前, 不会开始执行线程。The thread does not begin executing until the Thread.Start method is called. 执行从ThreadStartParameterizedThreadStart委托表示的方法的第一行开始。Execution begins at the first line of the method represented by the ThreadStart or ParameterizedThreadStart 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.

对于C++, 从 .NET Framework 2.0 开始, 为静态ThreadStart方法创建委托只需要一个参数: 由类名称限定的回调方法的地址。For C++, starting with .NET Framework 2.0, creating a ThreadStart delegate for a static method requires only one parameter: the address of the callback method, qualified by the class name. 在早期版本中, 为静态方法创建委托时需要两个参数: 零 (null) 和方法地址。In earlier versions two parameters were required when creating a delegate for a static method: zero (null) and the method address. 对于实例方法, 所有版本都需要两个参数: 实例变量和方法地址。For an instance method, all versions require two parameters: the instance variable and the method address.

扩展方法

GetMethodInfo(Delegate)

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

适用于

另请参阅