从不是创建控件的线程访问(C# winFrom)

时间:2022-07-26 18:22:35

跨线程访问控件,可以通过委托完成,

参考一下下面代码

 

publicdelegatevoid MyInvoke(string str); 




privatevoid button9_Click(object sender, EventArgs e)
{
//_myInvoke = new MyInvoke(SetText);
//CheckForIllegalCrossThreadCalls = false; Thread t =new Thread(new ThreadStart(fun));
t.Start();
}

privatevoid fun()
{
//_myInvoke("dddd"); SetText("ddd");
}
privatevoid SetText(string s)
{
if (textBox6.InvokeRequired)
{
MyInvoke _myInvoke
=new MyInvoke(SetText);
this.Invoke(_myInvoke, newobject[] { s });
}
else
{
this.textBox6.Text = s;
}
}