-1

我执行了一个循环 10 次的遗传算法,每次调用“FitnessFunction”函数时,我使用后台工作人员在拓扑上移动一个对象,一旦它到达特定点,然后我取消该后台工作人员并返回“遗传算法”功能...

不幸的是,我收到以下错误:

Cross-thread operation not valid: Control 'topology' accessed from a thread other than the thread it was created on...

我试过的:

每次我调用“FitnessFunction”函数时运行后台工作程序,该函数负责在特定条件下停止它。

4

1 回答 1

0

WinForms 禁止从主 UI 以外的其他线程访问控件。

使用Invoke(..)表单或使用SynchronizationContext类的方法。

private SynchronizationContext context;

MyForm()
{
  InitializeComponents();
  context = SynchronizationContext.Current;
}

///// somewhere in another thread
context.Post(myCallbackInUIThread, null) // you can use Send for synchronous call
于 2017-05-15T21:03:29.837 回答