1
내 주요 클래스 :WPF 스레드() 블록 UI 스레드
List<string> myList;
...
private void Button_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(() =>
{
myList = myClass.getListData(); // This takes for awhile.
});
thread.start();
thread.join(); // So I want my program to wait the thread is done here.
listBox1.ItemsSource = myList; // Then, update the listbox.
}
내가 Thread.join를을 (알고있다) 내 UI 스레드를 차단됩니다.
어떻게 예방합니까?
BackgroundWorker를 사용하여이 작업을 수행 할 수 있지만 스레드를 계속 사용하는 방법을 알고 싶습니다.
PLUS)
부분을 스레딩, 나는 다음과 같은 작업을 분리 할 수 있습니다 :
private void Button_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(() =>
{
myList = myClass.getListData();
Action uiAction =() => listBox1.ItemsSource = myList;
Dispatcher.Invoke(uiAction); // Execute on the UI thread
});
thread.Start();
}
가능한 경우 :
Thread thread = new Thread(() => {
myClass.doSomething(); // Adding data into a list in the other class.
myList = myClass.getList(); // Get the list from the other class.
});
와우 매우 빠른 응답입니다! 감사. 두 번째 예제에서 예제 코드 (또는 링크)를 줄 수 있습니까? 'System.Collections.Generic.List <..>'을 (를) 기다릴 수 없습니다.라는 오류 메시지가 나타납니다. – KimchiMan
@KimchiMan : 그게 무슨 소리 야? 당신은'GetListDataAsync'를 직접 구현해야합니다 ... 당신이하는 일에 대해 아무것도 말하지 않았습니다. 비동기 성은 손가락으로 간단히 "할 수있는"것이 아닙니다. 신중히 생각해야합니다. 그러나 장기간 실행되는 프로세스와 상호 작용하는 반응 형 UI를 만들려고 시도하는 경우에는 조사하는 것이 좋습니다. –
매우 유용한 링크를 발견했습니다. 고맙습니다! http://msdn.microsoft.com/en-us/library/hh191443.aspx – KimchiMan