C#用副線程去試圖修改主線程的UI控制項會報出異常,解決方案是使用副線程註冊事件通知主線程自己去修改UI控制項在winform中,方法如下private void button1_Click(object sender, EventArgs e){ Thread myThread = new Th...
C#用副線程去試圖修改主線程的UI控制項會報出異常,解決方案是使用副線程註冊事件通知主線程自己去修改UI控制項
在winform中,方法如下
private void button1_Click(object sender, EventArgs e)
{
Thread myThread = new Thread (new ThreadStart(fun));
myThread.Start();
}
private void fun()
{
textBox1.BeginInvoke(new Action(()=>{textBox1.Text="hello";}));
}
在WPF中,方法如下
1 private void button1_Click(object sender, EventArgs e) 2 { 3 Thread myThread = new Thread (new ThreadStart(fun)); 4 myThread.Start(); 5 } 6 7 private void fun() 8 { 9 TextBox1.Dispatcher.BeginInvoke(new Action(()=>{TextBox1.AppendText("hello");})); 10 }