2

문제는 간단합니다. GUI C#/xaml 앱이 있고 다른 스레드에서 GUI를 한 스레드와 일부 메소드 (무한 루프)에서 실행하려고합니다. 그리고 두 번째 스레드에서 GUI (목록 상자)의 요소를 수정해야합니다. 전역 변수와 웹에서 다른 팁을 만들려고했지만 아무 것도 잘 돌아 가지 않습니다."공유"목록 상자에 액세스 할 수있는 다중 스레드

는 지금은 같은 것을 가지고,이 어쩌면 잘 일을 할,하지만이 시도 할 수

public delegate void InvokeDelegate(listdata Mujlist); 
//in global scope 
// and in Window class 
public void UpdateList(listdata Mujlist) 
{ 
    mujlistbox.DataContext = Mujlist; 
} 
// and in the second thread this 
object[] obj = new object[1]; 
obj[0] = Mujlist; 
mujlistbox.BeginInvoke(new InvokeDelegate(UpdateList), obj); 

를 VS 2010 찾기 오류

Error 1 'System.Windows.Controls.ListBox' does not contain a definition for 'BeginInvoke' and no extension method 'BeginInvoke' accepting a first argument of type 'System.Windows.Controls.ListBox' could be found (are you missing a using directive or an assembly reference?) D:\..\MainWindows.xaml.cs 85 28 WPFChat 

하지만 System.Windows.Forms 때문에 method이므로 this과 혼동 스럽습니다.

그래서, 질문은 어떻게 단순히 "GUI 스레드"에서 자식 스레드에서 목록 상자를 업데이트 할 수 있습니까?

어디서 실수합니까? 어떻게 더 좋은 방법이 있나요? 방법?

+0

, 우리는 [그래서]에 "감사합니다", 또는 "어떤 도움 감사합니다", 또는 서명을 사용하지 않는 : 따라서, 당신이 Dispatcher에 접근하는 데 사용할 수있는 Dispatcher 속성이 있습니다 . "[안녕하세요, '고마워,'태그 라인 및 인사말을 게시물에서 삭제해야합니까?] (http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be 참조) -removed-from-posts). –

답변

3

에 표시된 대답을 참조하십시오, 당신은 Dispatcher.BeginInvoke 방법을 사용해야합니다.

ListBoxBeginInvoke 방법을 포함하지 않는 UIElement이고, DispatcherObject에서 파생됩니다. 포럼 사이트와는 달리

mujlistbox.Dispatcher.BeginInvoke(new InvokeDelegate(UpdateList), obj); 
0

BackGroundWorker을 사용하고 BackGroundWorker.DoWork 방법으로 장기 실행 작업을 구현하십시오. 진행보고를위한 처리기를 추가하고 진행 이벤트에서 UI를 업데이트하십시오. 훨씬 더 간단합니다.

는 WPF와 this 질문

관련 문제