C# 线程更新ui

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

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

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

示例 1

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

注意 说明:

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

C#
VB
 
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 控件的示例

C#
VB
 
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/archive/2012/08/06/2624775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值