wpf异步线程更新页面控件

说起来其实很简单,简单记录一下:

1、创建线程(也可以用Task)

Thread thread = new Thread(ChartInitail);
thread.Start();

2、更新主页面控件

this.Dispatcher.BeginInvoke(new Action(() =>
{
     //默认背景色
     SetTemp.Background = new SolidColorBrush(Color.FromRgb(112, 128, 144));
     CurTemp.Background = new SolidColorBrush(Color.FromRgb(25, 25, 112));
}

这里有个坑要避免踩到,this.Dispatcher.BeginInvoke里不要放执行很耗时的操作,否则会导致页面卡住,完全不能做任何操作,因为this代表的是主线程,主线程在执行很耗时的操作,整个页面必然会卡住动不了。

原文地址:https://www.cnblogs.com/wanggang2016/p/13717648.html