C# 线程更新ui

本部分包含两个示例。 第一个示例演示如何创建执行静态方法的线程。 第二个示例演示如何创建执行实例方法的线程。

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

有关创建线程的更多示例,请参见启动时创建线程并传递数据 有关使用等待句柄协调线程操作的示例,请参见 EventWaitHandle 有关使用临界区(在 C# 中为 lock,在 Visual Basic 中为 SyncLock)协调线程操作的示例,请参见Monitor 有关如何使用线程池线程的示例,请参见 BackgroundWorkerThreadPool 和 Timer

示例 1

下面的示例演示如何创建执行静态方法的线程。

注意 说明:

若要运行此示例,请参见生成使用 Demo 方法和 TextBlock 控件的示例

 
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

下面的示例演示如何创建执行实例方法的线程。

注意 说明:

若要运行此示例,请参见生成使用 Demo 方法和 TextBlock 控件的示例

 
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
原文地址:https://www.cnblogs.com/songtzu/p/2624775.html