在线程执行完成之后,在他的ContinueWith方法中加入Dispatcher.Invoke跨线程操作即可
var task = Task.Run(() => {
                    foreach (WordFind fl in list)
                    {
                        this.pro.Dispatcher.Invoke(() => this.pro.Value++);
                    }
                });
                task.ContinueWith(x=> {
                    this.Dispatcher.Invoke(() => {   //UI控件数据更新
                        loading.dataGrid.ItemsSource = es;
                        loading.Title = "失败:" + error;
                        loading.ShowDialog(this);
                        button_Copy.Content = "输出至:" + openFileDialog.SelectedPath + "(失败:" + error + ")";
                    });
                    
                });
                
- 本文标题: WPF之Task线程的运用,以及如何使用ContinueWith更新UI主线程
- 文章分类:【WinForm/WPF】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.