2017-05-15 5 views
-1

나는 "FitnessFunction"함수를 호출 할 때마다 백그라운드 작업자를 사용하여 토폴로지에서 개체를 이동하는 10 번 루프를 수행하는 유전 알고리즘을 수행했습니다. 내가 시도 무엇동일한 백그라운드 작업자를 실행하고 C에서 루프 내에서 중지하는 방법

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

: 그 다음 특정 지점에 도달하면 나는 불행하게도 다시 "Genetic_Algorithm"기능에 그 배경 노동자와 ...

을 취소, 나는 다음과 같은 오류가 발생했습니다

특정 조건에서 작업을 중지시키는 "FitnessFunction"함수를 호출 할 때마다 백그라운드 작업자를 실행하십시오.

+0

컨트롤을 업데이트 할 수있는 UI 스레드에서 이벤트를 발생시키는'ReportProgress()'함수를 살펴보아야합니다. 그렇지 않으면 UI 컨트롤 호출이 만들어진 UI 스레드에 마샬링되어야합니다. https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.reportprogress(v=vs.110).aspx – xxbbcc

+0

@xxbbcc : 답변을 제출해 주시겠습니까? – Rose

답변

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 
+0

수정하는 방법 (true), 사용 방법은 무엇입니까? 당신은 당신의 대답을 끝낼 수 있습니까? – Rose

+0

비 UI 스레드 (작업자)로부터 컨트롤을 그릴 때 예외가 발생합니다. 다른 방법으로 도면 작업을 래핑하고 호출/게시를 통해 호출합니다. – rattler