在C# 的應用程序開發中, 我們經常要把UI線程和工作線程分開,防止界面停止響應。 同時我們又需要在工作線程中更新UI界面上的控件,
下面介紹幾種常用的方法
閱讀目錄
- 線程間操作無效
- 第一種辦法:禁止編譯器對跨線程訪問做檢查
- 第二種辦法: 使用delegate和invoke來從其他線程中調用控件
- 第三種辦法: 使用delegate和BeginInvoke來從其他線程中控制控件
- 第四種辦法: 使用BackgroundWorker組件
- 源代碼下載
線程間操作無效
界面上有一個button和一個label, 點擊button會啟動一個線程來更新Label的值
private void button1_Click(object sender, EventArgs e) { Thread thread1 = new Thread(new ParameterizedThreadStart(UpdateLabel)); thread1.Start("更新Label"); }</span><span style="color:rgb(0,0,255); line-height:1.5!important">private</span> <span style="color:rgb(0,0,255); line-height:1.5!important">void</span> UpdateLabel(<span style="color:rgb(0,0,255); line-height:1.5!important">object</span><span style="line-height:1.5!important"> str) { </span><span style="color:rgb(0,0,255); line-height:1.5!important">this</span>.label1.Text =<span style="line-height:1.5!important"> str.ToString(); }</span></pre>
運行后, 程序會報錯 "跨線程操作無效,從不是創建"label1"的線程訪問它"
這是因為.NET禁止了跨線程調用控件, 否則誰都可以操作控件,最后可能造成錯誤。
下面介紹幾種跨線程調用控件的方法
第一種辦法:禁止編譯器對跨線程訪問做檢查
這是最簡單的辦法, 相當於不檢查線程之間的沖突,允許各個線程隨便亂搞,最后Lable1控件的值是什么就難以預料了 (不推薦使用這種方法)
public Form1() { InitializeComponent(); // 加入這行 Control.CheckForIllegalCrossThreadCalls = false; }
第二種辦法: 使用delegate和invoke來從其他線程中調用控件
調用控件的invoke方法,就可以控制控件了,例如
private void button2_Click(object sender, EventArgs e) { Thread thread1 = new Thread(new ParameterizedThreadStart(UpdateLabel2)); thread1.Start("更新Label"); }</span><span style="color:rgb(0,0,255); line-height:1.5!important">private</span> <span style="color:rgb(0,0,255); line-height:1.5!important">void</span> UpdateLabel2(<span style="color:rgb(0,0,255); line-height:1.5!important">object</span><span style="line-height:1.5!important"> str) { </span><span style="color:rgb(0,0,255); line-height:1.5!important">if</span><span style="line-height:1.5!important"> (label2.InvokeRequired) { </span><span style="color:rgb(0,128,0); line-height:1.5!important">//</span><span style="color:rgb(0,128,0); line-height:1.5!important"> 當一個控件的InvokeRequired屬性值為真時,說明有一個創建它以外的線程想訪問它</span> Action<<span style="color:rgb(0,0,255); line-height:1.5!important">string</span>> actionDelegate = (x) => { <span style="color:rgb(0,0,255); line-height:1.5!important">this</span>.label2.Text =<span style="line-height:1.5!important"> x.ToString(); }; </span><span style="color:rgb(0,128,0); line-height:1.5!important">//</span><span style="color:rgb(0,128,0); line-height:1.5!important"> 或者 </span><span style="color:rgb(0,128,0); line-height:1.5!important">//</span><span style="color:rgb(0,128,0); line-height:1.5!important"> Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; };</span> <span style="color:rgb(0,0,255); line-height:1.5!important">this</span><span style="line-height:1.5!important">.label2.Invoke(actionDelegate, str); } </span><span style="color:rgb(0,0,255); line-height:1.5!important">else</span><span style="line-height:1.5!important"> { </span><span style="color:rgb(0,0,255); line-height:1.5!important">this</span>.label2.Text =<span style="line-height:1.5!important"> str.ToString(); } }</span></pre>
第三種辦法: 使用delegate和BeginInvoke來從其他線程中控制控件
只要把上面的 this.label2.Invoke(actionDelegate, str); 中的 Invoke 改為BeginInvoke方法就可以了
Invoke方法和BeginInvoke方法的區別是
Invoke方法是同步的, 它會等待工作線程完成,
BeginInvoke方法是異步的, 它會另起一個線程去完成工作線程
第四種辦法: 使用BackgroundWorker組件(推薦使用這個方法)
BackgroundWorker是.NET里面用來執行多線程任務的控件,它允許編程者在一個單獨的線程上執行一些操作。耗時的操作(如下載和數據庫事務)。用法簡單
private void button4_Click(object sender, EventArgs e) { using (BackgroundWorker bw = new BackgroundWorker()) { bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.RunWorkerAsync("Tank"); } }</span><span style="color:rgb(0,0,255); line-height:1.5!important">void</span> bw_DoWork(<span style="color:rgb(0,0,255); line-height:1.5!important">object</span><span style="line-height:1.5!important"> sender, DoWorkEventArgs e) { </span><span style="color:rgb(0,128,0); line-height:1.5!important">//</span><span style="color:rgb(0,128,0); line-height:1.5!important"> 這里是后台線程, 是在另一個線程上完成的 </span><span style="color:rgb(0,128,0); line-height:1.5!important">//</span><span style="color:rgb(0,128,0); line-height:1.5!important"> 這里是真正做事的工作線程 </span><span style="color:rgb(0,128,0); line-height:1.5!important">//</span><span style="color:rgb(0,128,0); line-height:1.5!important"> 可以在這里做一些費時的,復雜的操作</span> Thread.Sleep(<span style="color:rgb(128,0,128); line-height:1.5!important">5000</span><span style="line-height:1.5!important">); e.Result </span>= e.Argument + <span style="color:rgb(128,0,0); line-height:1.5!important">"</span><span style="color:rgb(128,0,0); line-height:1.5!important">工作線程完成</span><span style="color:rgb(128,0,0); line-height:1.5!important">"</span><span style="line-height:1.5!important">; } </span><span style="color:rgb(0,0,255); line-height:1.5!important">void</span> bw_RunWorkerCompleted(<span style="color:rgb(0,0,255); line-height:1.5!important">object</span><span style="line-height:1.5!important"> sender, RunWorkerCompletedEventArgs e) { </span><span style="color:rgb(0,128,0); line-height:1.5!important">//</span><span style="color:rgb(0,128,0); line-height:1.5!important">這時后台線程已經完成,並返回了主線程,所以可以直接使用UI控件了 </span> <span style="color:rgb(0,0,255); line-height:1.5!important">this</span>.label4.Text =<span style="line-height:1.5!important"> e.Result.ToString(); }</span></pre>
轉自https://www.cnblogs.com/lonelyxmas/p/4097737.html