System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; 在初始化设置这个属性,系统的控件就不会抛出异常了
// 委托
private delegate void mySetState(bool bState);//声明委托
// 实现
private void threadSetState(bool bState)
{
if (this.InvokeRequired)
{
mySetState st = new mySetState(threadSetState);
Invoke(st, new object[] { bState });
}
else
{
buttonBegin.Enabled = bState;
}
}
在线程中调用threadSetState函数修改控件
Invoke(new Action(() => {
buttonBegin.Enabled = true;
}));
buttonBegin.BeginInvoke(new Action(() => { buttonBegin.Enabled = false; }));