在WPF应用程序中使用多线程的方式与Windows Forms很类似,区别在于,如果需要更新主线程UI上面的元素,需要用一个特殊的方法(this.Dispatcher.Invoke)

下面是一个简单的范例,演示了如何执行一个简单的方法,它使用了callback的机制进行多线程异步执行。

            Func<string> func = new Func<string>(() =>
            {
                Thread.Sleep(5000);
                return "Hello,world";
            });

            AsyncCallback callback = new AsyncCallback((i) =>
            {
                this.Dispatcher.Invoke(
                    new Action(() =>
                    {
                        this.Title = func.EndInvoke(i);
                    }));
            });
            func.BeginInvoke(callback, null);

相关文章:

  • 2022-12-23
  • 2021-12-25
  • 2021-05-14
  • 2021-12-10
  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-07
  • 2021-11-24
  • 2022-12-23
  • 2021-12-25
  • 2021-12-25
相关资源
相似解决方案