我有一個用戶控件與樹上。它使用多線程向其添加節點。我有一個函數稱爲展開,我需要執行過濾完成後,因爲我是一個多線程的新手我不知道如何做到這一點。這裏是我的代碼:等待的線程問題
class MyClass : UserControl
{
private Thread nThread;
private bool searchLoadCompleted = false;
private void Filter()
{
ClearTree();
this.nThread = new Thread(new ParameterizedThreadStart(AddFilteredResultsToTree));
this.nThread.IsBackground = true;
this.nThread.Start(someParameter);
}
private void AddFilteredResultsToTree(int someParameter)
{
myTree.Invoke(new MethodInvoker(()=> this.searchLoadCompleted = false));
myTree.Invoke(new MethodInvoker(()=> AppendNode(......)));
myTree.Invoke(new MethodInvoker(()=> this.searchLoadCompleted = true));
}
private void Expand()
{
}
}
我嘗試添加nThread.Join()
到Expand()
但它得到無限期地卡住。我該怎麼辦?
爲什麼不把Expand()代碼放到AddFilteredResultsToTree()中? – 2009-11-18 14:12:08