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代表的是主線程,主線程在執行很耗時的操作,整個頁面必然會卡住動不了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM