[問題]WPF 如何在backgroundworker 跨執行緒
如果非得在DoWork中跨UI執行緒,又不想鎖main thread
有沒有比較好的建議,謝謝
private void Retrive()
{
//thread.sleep 當作是畫面繪製的工作
System.Threading.Thread.Sleep(5000);
this.txt.Text = "Complete!";
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
BackgroundWorker bgn = new BackgroundWorker();
bgn.DoWork += (s1, e1) => {
functiondelegate funtionwork = new functiondelegate(Retrive);
//LOCK UI
this.Dispatcher.BeginInvoke(funtionwork, DispatcherPriority.Appl
icationIdle);
};
bgn.RunWorkerCompleted += (s1, e1) => {
this.txt.Text = "OK!";
};
bgn.RunWorkerAsync();
}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.53.109
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1469033546.A.CC1.html
※ 編輯: eggeggss (36.224.53.109), 07/21/2016 00:53:48
※ 編輯: eggeggss (36.224.53.109), 07/21/2016 00:54:20
推
07/21 00:58, , 1F
07/21 00:58, 1F
→
07/21 03:05, , 2F
07/21 03:05, 2F
→
07/21 03:06, , 3F
07/21 03:06, 3F
→
07/21 03:06, , 4F
07/21 03:06, 4F
是啊因為有一部分處理耗時的UI,但又不想LOCK畫面..
※ 編輯: eggeggss (118.163.51.253), 07/21/2016 09:12:45
→
07/21 09:15, , 5F
07/21 09:15, 5F
→
07/21 09:17, , 6F
07/21 09:17, 6F
→
07/21 09:17, , 7F
07/21 09:17, 7F
thread.sleep 那段當成是在畫面上繪製excel圖表,我加上註解
找過很多解答都跟你的答案一樣
但若是UI Thread 要做很久,怎麼處理呢
※ 編輯: eggeggss (118.163.51.253), 07/21/2016 09:46:54
→
07/21 09:45, , 8F
07/21 09:45, 8F
→
07/21 09:46, , 9F
07/21 09:46, 9F
→
07/21 09:46, , 10F
07/21 09:46, 10F
我自己知道怎麼區分,但若要包給其他不想或不知道這樣區分的人,有沒有別的做法呢
※ 編輯: eggeggss (118.163.51.253), 07/21/2016 09:49:26
推
07/21 12:23, , 11F
07/21 12:23, 11F
→
07/21 12:24, , 12F
07/21 12:24, 12F
→
07/21 12:24, , 13F
07/21 12:24, 13F
this就可以直接調用了
※ 編輯: eggeggss (118.163.51.253), 07/21/2016 13:39:41
※ 編輯: eggeggss (118.163.51.253), 07/21/2016 13:40:07
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章