信息
您所需的主题如下所示。但此主题未包含在此库中。

ThreadStart 委托

2013/12/13

表示在 Thread 上执行的方法。

Namespace:  System.Threading
程序集:  mscorlib(位于 mscorlib.dll 中)

public delegate void ThreadStart()

在创建托管的线程时,在该线程上执行的方法将通过一个传递给 Thread 构造函数的 ThreadStart 委托或 ParameterizedThreadStart 委托来表示。在调用 Thread.Start 方法之前,该线程不会开始执行。执行将从 ThreadStartParameterizedThreadStart 委托表示的方法的第一行开始。

说明注意:

Visual Basic 和 C# 用户在创建线程时可以省略 ThreadStartParameterizedThreadStart 委托构造函数。在 Visual Basic 中,将方法传递给 Thread 构造函数时使用 AddressOf 运算符,例如 Dim t As New Thread(AddressOf ThreadProc)。在 C# 中,只需指定线程过程的名称。编译器会选择正确的委托构造函数。

本部分包含两个示例。第一个示例演示如何使用 ThreadStart 委托执行静态方法,第二个示例演示如何使用 ThreadStart 委托执行实例方法。

这些示例在 UI 线程上的 TextBlock 中显示它们的输出。为了从回调线程访问 TextBlock,这些示例使用 Dispatcher 属性来获取 TextBlockDispatcher 对象,然后使用 Dispatcher.BeginInvoke 方法进行跨线程调用。

有关创建线程的更多信息,请参见[52B32222-E185-4F42-91A7-EACA65C0AB6D]

示例 1

下面的代码示例演示如何使用 ThreadStart 委托执行静态方法。


using System;
using System.Threading;

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      // To start a thread using a static thread procedure, use the
      // class name and method name when you create the ThreadStart
      // delegate. C# expands the method name to the appropriate 
      // delegate creation syntax:
      //    New ThreadStart(Example.DoWork)
      //
      Thread newThread = new Thread(Example.DoWork);
      newThread.Start();
   }

   // Simulate work. To communicate with objects on the UI thread, get the 
   // Dispatcher for one of the UI objects. Use the Dispatcher object's 
   // BeginInvoke method to queue a delegate that will run on the UI thread,
   // and therefore can safely access UI elements like the TextBlock.
   private static void DoWork()
   {
      outputBlock.Dispatcher.BeginInvoke(delegate () { 
         outputBlock.Text += "Hello from a static thread procedure.\n"; 
      });
   }
}

/* This code example produces the following output:

Hello from a static thread procedure.
 */


示例 2

下面的代码示例演示如何使用 ThreadStart 委托执行实例方法。


using System;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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. C# expands the object
      // reference and method name to the appropriate delegate 
      // creation syntax:
      //    New ThreadStart(AddressOf w.DoMoreWork)
      //
      Work w = new Work();
      w.Data = 42;
      w.Output = outputBlock;

      Thread newThread = new Thread(w.DoMoreWork);
      newThread.Start();
   }
}

public class Work
{
   public int Data;
   public System.Windows.Controls.TextBlock Output;

   // Simulate work. To communicate with objects on the UI thread, get the 
   // Dispatcher for one of the UI objects. Use the Dispatcher object's 
   // BeginInvoke method to queue a delegate that will run on the UI thread,
   // and therefore can safely access UI elements like the TextBlock.
   public void DoMoreWork()
   {
      Output.Dispatcher.BeginInvoke(delegate () {
         Output.Text += String.Format("Instance thread procedure. Data={0}\n", Data);
      });
   }
}

// This code example produces the following output:
//
//Instance thread procedure. Data=42


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: