参考:https://www.cnblogs.com/lvdongjie/p/5428815.html
.Net 通常禁止跨线程访问控件,设置Control.CheckForIllegalCrossThreadCalls = false 可以解除禁止,但不能保证正确跨线程访问控件。
使用MethodInvoker处理:
MethodInvoker自身即为委托事件
# new MethodInvoker( A ) 添加事件A
示例:
// txt winfrom text控件
private void btnOK_Click(object sender, EventArgs e)
{
txt.Text = "创建线程.....";
Thread td = new Thread(new ThreadStart(run));
td.Start();
}
//运行方法
private void run()
{
this.txt.Text = "线程就绪";
}
修改为:
private void btnOK_Click(object sender, EventArgs e)
{
txt.Text = "创建线程.....";
Thread td = new Thread(new ThreadStart(threadRun));
td.Start();
}
// 运行方法
private void run()
{
this.txt.Text = "线程就绪";
}
/// <summary>
/// 线程方法
/// </summary>
private void threadRun()
{
MethodInvoker In = new MethodInvoker(run);
this.BeginInvoke(In);
}