Windows
Forms 控件通常不是thread-safe(直接或間接繼承于System.Windows.Forms.Control),因此.NET
Framework為防止multithread下對控件的存取可能導致控件狀態(tài)的不一致,在調試時,CLR-Debugger會拋出一個
InvalidOperationException以‘建議‘程序員程序可能存在的風險。
問題的關鍵在于,動機是什么?和由此而來的編程模型的調整。
首先,看一個代碼實例。該例要完成的工作是由一個Button的Click觸發(fā),啟動一個Thread(Manual Thread),該Thread的目的是完成設置TextBox的Text’s Property。
Code 1.1
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void unsafeSetTextButton_Click(object sender, EventArgs e) {
Thread setTextThread = new Thread(new ThreadStart(doWork));
setTextThread.Start();
}
private void doWork() {
string fileName = ".\\test-src.txt";
if (!File.Exists(fileName)) {
MessageBox.Show(string.Format("{0} doesn‘t exist!", fileName),
"FileNoFoundException");
return;
}
string text = null;
using (StreamReader reader = new StreamReader(fileName, Encoding.Default)) {
text = reader.ReadToEnd();
}
this.textBox1.Text = text;
}
}
}
在調試時,CLR-Debugger會在以上代碼中粗體處將會彈出如下的對話框:
![]() 提示說,當前存取控件的thread非創(chuàng)建控件的thread(Main Thread)。
當
然,你也可以忽略InvalidOperationException,在非調試的狀態(tài)下,該異常并不會被拋出,CLR-Debugger監(jiān)測對
Handle的可能存在的不一致地存取,而期望達到更穩(wěn)健(robust)的代碼,這也就是Cross-thread operation not
valid后的真正動機。
但是,放在面前的選擇有二:第一,在某些情況下,我們并不需要這種善意的‘建議
‘,而這種建議將在調試時帶來了不必要的麻煩;第二,順應善意的‘建議‘,這也意味著我們必須調整已往行之有效且得心應手的編程模型(成本之一),而這種
調整額外還會帶來side-effect,而這種side-effect目前,我并不知道有什么簡潔優(yōu)雅的解決之道予以消除(成本之二)。
忽略Cross-thread InvalidOperationException建議,前提假設是我們不需要類似的建議,同時也不想給自己的調試帶來過多的麻煩。
關
閉CheckForIllegalCrossThreadCalls,這是Control class上的一個static
property,默認值為flase,目的在于開關是否對Handle的可能存在的不一致存取的監(jiān)測;且該項設置是具有Application
scope的。
如果,只需要在某些Form中消除Cross-thread
InvalidOperationException建議,可以在Form的.ctor中,InitializeComponent語句后將
CheckForIllegalCrossThreadCalls設置為false 。
Code 2. - 1
public Form1() {
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
這種方式雖然可以達到忽略Cross-thread InvalidOperationException建議的目的,但代碼不能明晰的表達具有Application scope的語義,下面方式能更好的表達Application scope語義而且便于維護。
Code 2. - 2
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Control.CheckForIllegalCrossThreadCalls = false;
Application.Run( new Form1() );
}
接受Cross-thread InvalidOperationException善意的建議,這通常是個明智的選擇,即使目前沒有簡潔優(yōu)雅的code pattern。
Code 3. – 1
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
}
private void safeSetTextButton_Click(object sender, EventArgs e) {
Thread safeSetTextThread = new Thread(new ThreadStart(doWork));
safeSetTextThread.Start();
}
private void doWork() {
string fileName = ".\\test-src.txt";
if (!File.Exists(fileName)) {
MessageBox.Show(string.Format("{0} doesn‘t exist!", fileName),
"FileNoFoundException");
return;
}
string text = null;
using (StreamReader reader = new StreamReader(fileName, Encoding.Default)) {
text = reader.ReadToEnd();
}
//this.textBox1.Text = text;
safeSetText(text);
}
private void safeSetText(string text) {
if (this.textBox1.InvokeRequired) {
_SafeSetTextCall call = delegate(string s) {
this.textBox1.Text = s;
};
this.textBox1.Invoke(call, text);
}
else
this.textBox1.Text = text;
}
private delegate void _SafeSetTextCall(string text);
}
}
其
中主要利用System.ComponentModel.IsynchronizeInvoke的InvokeRequired和Invoke
方法(System.Windows.Forms.Control繼承于此),該code
pattern對于大多數(shù)Windows控件有效 ;這樣做的目的是保證由創(chuàng)建控件的Main
Thread唯一性地呼叫get_Handle。(注意Code 3. -1 中的粗體 safeSetText方法)
但,System.Windows.Forms中ToolStripItem繼承鏈上的控件并不具有后向兼容性,因此以上code pattern對此類控件不適用;可以將以上code pattern改為如下:
private void safeSetText(string text) {
if (this.InvokeRequired) {
_SafeSetTextCall call = delegate(string s) {
this.textBox1.Text = s;
};
this.Invoke(call, text);
}
else
this.textBox1.Text = text;
}
private delegate void _SafeSetTextCall(string text);
因為System.Windows.Form繼承System.Windows.Control,可以保證以上代碼可以正確編譯也能正常按期望工作,這樣一來,代碼的彈性會好些。
國
外有兄弟利用Reflection技術將設置單一屬性(Property)完全動態(tài)化了,代碼的彈性因此也更好,但我不鼓勵這種做法。理由有二:第一,之
所以采用Multithread是因為需要更好的UI反應(interactive)、或者更好的性能、或者兩者都要,在這種前提下,
Reflection似乎與目標背道而馳;第二,目前這種實現(xiàn)技術所帶來的代碼彈性的提升非常有限;不過有興趣的,可以自己驗證一下。
|
|
來自: ShangShujie > 《.net》