Thread thread = new Thread(new ThreadStart(Receive));
// 我也用过这个Thread thread = new Thread(new ParameterThreadStart(Receive));也不行。
thread.Start();Private void Receive()
{
textBox1.text = "aaa"; // 它老是在这里报错,说不能什么通过线程访问什么
//Cross thread operation not valid: Control "textBox1" accessed from a thread other than the thread it was created on.
}望高手指点
private void SetText(string text)
{
if (textBox1.InvokeRequired)
{
AddListBoxTextCallback d = new AddListBoxTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
textBox1.text = text;
}
}
你的程序改为
Private void Receive()
{
SetText(string text)="aaa"); // 它老是在这里报错,说不能什么通过线程访问什么
//Cross thread operation not valid: Control "textBox1" accessed from a thread other than the thread it was created on.
}
应为SetTextCallbac d=new SetTextCallbac(setText)
{
textBox1.text = value;
}// 定义委托
UpdateTextBoxHandle updateInThread = new UpdateTextBoxHandle(SetTextBox);Private void Receive()
{
// 通过委托刷新
updateInThread("aaa");
}你试试看行不行
public delegate void InvokeDelegate();private void Invoke_Click(object sender, EventArgs e)
{
myTextBox.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
public void InvokeMethod()
{
myTextBox.Text = "Executed the given delegate";
}
CheckForIllegalCrossThreadCalls = false;PS:1楼湖南人啊?
可以手动关闭次功能CheckForIllegalCrossThreadCalls = false;
2楼和5楼是安全的方法.