Windows
Forms 控件通常不是thread-safe(直接或間接繼承于System.Windows.Forms.Control),因此.NET
Framework為防止multithread下對控件的存取可能導(dǎo)致控件狀態(tài)的不一致,在調(diào)試時(shí),CLR-Debugger會(huì)拋出一個(gè)
InvalidOperationException以‘建議‘程序員程序可能存在的風(fēng)險(xiǎn)。
問題的關(guān)鍵在于,動(dòng)機(jī)是什么?和由此而來的編程模型的調(diào)整。
首先,看一個(gè)代碼實(shí)例。該例要完成的工作是由一個(gè)Button的Click觸發(fā),啟動(dòng)一個(gè)Thread(Manual Thread),該Thread的目的是完成設(shè)置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;
}
}
}
在調(diào)試時(shí),CLR-Debugger會(huì)在以上代碼中粗體處將會(huì)彈出如下的對話框:
![]() 提示說,當(dāng)前存取控件的thread非創(chuàng)建控件的thread(Main Thread)。
當(dāng)
然,你也可以忽略InvalidOperationException,在非調(diào)試的狀態(tài)下,該異常并不會(huì)被拋出,CLR-Debugger監(jiān)測對
Handle的可能存在的不一致地存取,而期望達(dá)到更穩(wěn)健(robust)的代碼,這也就是Cross-thread operation not
valid后的真正動(dòng)機(jī)。
但是,放在面前的選擇有二:第一,在某些情況下,我們并不需要這種善意的‘建議
‘,而這種建議將在調(diào)試時(shí)帶來了不必要的麻煩;第二,順應(yīng)善意的‘建議‘,這也意味著我們必須調(diào)整已往行之有效且得心應(yīng)手的編程模型(成本之一),而這種
調(diào)整額外還會(huì)帶來side-effect,而這種side-effect目前,我并不知道有什么簡潔優(yōu)雅的解決之道予以消除(成本之二)。
忽略Cross-thread InvalidOperationException建議,前提假設(shè)是我們不需要類似的建議,同時(shí)也不想給自己的調(diào)試帶來過多的麻煩。
關(guān)
閉CheckForIllegalCrossThreadCalls,這是Control class上的一個(gè)static
property,默認(rèn)值為flase,目的在于開關(guān)是否對Handle的可能存在的不一致存取的監(jiān)測;且該項(xiàng)設(shè)置是具有Application
scope的。
如果,只需要在某些Form中消除Cross-thread
InvalidOperationException建議,可以在Form的.ctor中,InitializeComponent語句后將
CheckForIllegalCrossThreadCalls設(shè)置為false 。
Code 2. - 1
public Form1() {
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
這種方式雖然可以達(dá)到忽略Cross-thread InvalidOperationException建議的目的,但代碼不能明晰的表達(dá)具有Application scope的語義,下面方式能更好的表達(dá)Application scope語義而且便于維護(hù)。
Code 2. - 2
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Control.CheckForIllegalCrossThreadCalls = false;
Application.Run( new Form1() );
}
接受Cross-thread InvalidOperationException善意的建議,這通常是個(gè)明智的選擇,即使目前沒有簡潔優(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唯一性地呼叫g(shù)et_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);
因?yàn)镾ystem.Windows.Form繼承System.Windows.Control,可以保證以上代碼可以正確編譯也能正常按期望工作,這樣一來,代碼的彈性會(huì)好些。
國
外有兄弟利用Reflection技術(shù)將設(shè)置單一屬性(Property)完全動(dòng)態(tài)化了,代碼的彈性因此也更好,但我不鼓勵(lì)這種做法。理由有二:第一,之
所以采用Multithread是因?yàn)樾枰玫腢I反應(yīng)(interactive)、或者更好的性能、或者兩者都要,在這種前提下,
Reflection似乎與目標(biāo)背道而馳;第二,目前這種實(shí)現(xiàn)技術(shù)所帶來的代碼彈性的提升非常有限;不過有興趣的,可以自己驗(yàn)證一下。
|
|