這是代碼:如何等待兩個BackgrandWorker的RunWorkerCompleted事件完成?
private void button1_Click(object sender, EventArgs e)
{
ParaClass pcs = new ParaClass();
pcs.strPath = textBox1.Text;
pcs.sendedGrid = ugSrc;
this.backgroundWorker1.RunWorkerAsync(pcs);
ParaClass pcsB = new ParaClass();
pcsB.strPath = textBox2.Text;
pcsB.sendedGrid = ultraGrid2;
this.backgroundWorker2.RunWorkerAsync(pcsB);
doSomething();
}
,並在這兩個backgrandworker1 & backgrandworker2「完滿成功事件,我這樣寫代碼:
private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk1();
}
private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk2();
}
現在的問題是:在Button1的的點擊事件的函數DoSomething的()必須等待雙方完成事件的完成。 如果我改變DoSomething的()來
private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk2();
doSomething();
}
然後,因爲有兩個線程,我不知道哪個線程將首先完成,所以如何解決
爲什麼不乾脆讓backgroundworker2正常的方法,而不是異步。 – CathalMF 2013-03-28 09:24:55
或者你可以使用AsyncCallBack委託http://msdn.microsoft.com/en-GB/library/system.asynccallback(v=vs.90).aspx 這裏是使用異步回調http://的一個很好的例子stackoverflow.com/questions/1047662/what-is-asynccallback – CathalMF 2013-03-28 09:26:38