这个方法简单暴力适合小工作量的修改一些界面内容.使用Dispatcher.BeginInvoke()会将代码安排为调度程序的一个任务.

步骤

  1. 使用Thread新建并开始一个线程
  2. 在新建的线程处理函数中需要修改界面的时候获取界面的dispatcher
  3. 使用Dispatcher的BeginInvoke方法指定一个线程优先级,和一个委托,这个委托时用于修改界面内容的

下面给出一部分代码

//新建线程
Thread thread = new Thread(UpdateTextRight);
thread.Start();

下面是新线程中的方法

//这个事例刚好是先窗体类中定义的,所以获取Dispatcher变得比较方便,而且使用了匿名委托.在通常的代码中会把委托给分离出去比较好.
private void UpdateTextRight()
{
    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
        (ThreadStart) delegate()
        {
            txt.Text = "Here is some new text.";
        }
        );
}

相关文章:

  • 2022-12-23
  • 2021-06-14
  • 2021-11-06
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-07
  • 2022-12-23
  • 2022-02-20
  • 2021-09-17
  • 2022-12-23
相关资源
相似解决方案