我在一个线程内设置textbox.text
出错InvalidOperationException was unhandled
Illegal cross-thread operation:
Control 'textBox1' accessed from a thread other than the threa it wa created on
出错InvalidOperationException was unhandled
Illegal cross-thread operation:
Control 'textBox1' accessed from a thread other than the threa it wa created on
如:
fnText()
{
textBox1 //在这里处理
}fn线程方法()
{
this.Invoke(fnText)
}thread th = new Thread(new ThreadStart(fn线程方法));th.start()以上就可以了。
{
if (form.InvokeRequired)
form.Invoke(new MethodInvoker(WorkerThread));
else
{
//do sth
}
}
请注意WorkerThread里有个技巧, if (form.InvokeRequired) 即如果当前线程不是创建该form的线程,则将方法通通过过Invoke方法放到UI线程里去执行。