首先來看傳統(tǒng)方法:
publicpartialclass Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
privatevoid Form1_Load(object sender, EventArgs e)
...{
Thread thread =new Thread(ThreadFuntion);
thread.IsBackground =true;
thread.Start();
}
privatevoid ThreadFuntion()
...{
while (true)
...{
this.textBox1.Text = DateTime.Now.ToString();
Thread.Sleep(1000);
}
}
}
運(yùn)行這段代碼,我們會(huì)看到系統(tǒng)拋出一個(gè)異常:Cross-thread operation not valid:Control 'textBox1' accessed from a thread other than the thread it was created on . 這是因?yàn)?net 2.0以后加強(qiáng)了安全機(jī)制,不允許在winform中直接跨線程訪問控件的屬性。那么怎么解決這個(gè)問題呢,下面提供幾種方案。
第一種方案,我們?cè)贔orm1_Load()方法中加一句代碼:
privatevoid Form1_Load(object sender, EventArgs e)
...{
Control.CheckForIllegalCrossThreadCalls =false;
Thread thread =new Thread(ThreadFuntion);
thread.IsBackground =true;
thread.Start();
}
加入這句代碼以后發(fā)現(xiàn)程序可以正常運(yùn)行了。這句代碼就是說在這個(gè)類中我們不檢查跨線程的調(diào)用是否合法(如果沒有加這句話運(yùn)行也沒有異常,那么說明系統(tǒng)以及默認(rèn)的采用了不檢查的方式)。然而,這種方法不可取。我們查看CheckForIllegalCrossThreadCalls 這個(gè)屬性的定義,就會(huì)發(fā)現(xiàn)它是一個(gè)static的,也就是說無論我們?cè)陧?xiàng)目的什么地方修改了這個(gè)值,他就會(huì)在全局起作用。而且像這種跨線程訪問是否存在異常,我們通常都會(huì)去檢查。如果項(xiàng)目中其他人修改了這個(gè)屬性,那么我們的方案就失敗了,我們要采取另外的方案。
下面來看第二種方案,就是使用delegate和invoke來從其他線程中控制控件信息。網(wǎng)上有很多人寫了這種控制方式,然而我看了很多這種帖子,表明上看來是沒有什么問題的,但是實(shí)際上并沒有解決這個(gè)問題,首先來看網(wǎng)絡(luò)上的那種不完善的方式:
publicpartialclass Form1 : Form
...{
privatedelegatevoid FlushClient();//代理
public Form1()
...{
InitializeComponent();
}
privatevoid Form1_Load(object sender, EventArgs e)
...{
Thread thread =new Thread(CrossThreadFlush);
thread.IsBackground=true;
thread.Start();
}
privatevoid CrossThreadFlush()
...{
//將代理綁定到方法
FlushClient fc =new FlushClient(ThreadFuntion);
this.BeginInvoke(fc);//調(diào)用代理
}
privatevoid ThreadFuntion()
...{
while (true)
...{
this.textBox1.Text = DateTime.Now.ToString();
Thread.Sleep(1000);
}
}
}