使用方法invoker解决线程交叉访问控制
. net原则上禁止线程交叉访问控制。 由于这可能会导致错误,因此有一种方法可以禁止编译器检查线程交叉访问。 control.checkforillegalcrossthreadcalls=false; 可以访问,但如果不发生错误,则C#无法保证线程交叉访问控制的运行时错误。 用MethodInvoker解决:
原始代码:
privatevoidbtnok _ click (对象发送器,事件e ) )。
{
tslInfo.Text='请稍等. ';
thread TD=new thread (newthreadstart (run ) );
td.Start (;
}
///summary
//线程方法
////summary
私有语音(
{
this.tslInfo.Text='就绪';
}
修改后:
privatevoidbtnok _ click (对象发送器,事件e ) )。
{
tslInfo.Text='请稍等. ';
thread TD=new thread (newthreadstart ) threadrun );
td.Start (;
}
///summary
//原始线程方法
////summary
私有语音(
{
this.tslInfo.Text='就绪';
}
///summary
//线程方法
////summary
私密性voidthreadrun (
{
methodinvokerin=new method invoker (run;
this.Begininvoke(in;
}