0

That's the code:

   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();
    }

and in both backgrandworker1 & backgrandworker2 ' complet event ,i write code like this:

    private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
    {
           doSomethingelsebk1();
    }

    private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
    {
           doSomethingelsebk2();
    }

now the problem is : the function doSomething() in button1's click event must wait both backgrandworker's complete event finish. if i change doSomething() to

    private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
    {
           doSomethingelsebk2();
           doSomething();  
    }

then,because there are two thread,i don't know which thread will finish first,so what is the solution

  • Why not just make backgroundworker2 a normal method and not Async. – CathalMF Mar 28 '13 at 09:24
  • Or you could use the AsyncCallBack Delegate http://msdn.microsoft.com/en-GB/library/system.asynccallback(v=vs.90).aspx Here is a good example of using async Callback http://stackoverflow.com/questions/1047662/what-is-asynccallback – CathalMF Mar 28 '13 at 09:26

2 Answers2

0

Create 2 flags which represent complete state of 2 BackgroundWorker.

Turn each flag on in the RunWorkerCompleted event, then call doSomething() method.

In doSomething method, check if both flags is on, then continue to do, otherwise, return.

Tu Tran
  • 1,957
  • 1
  • 27
  • 50
0

Create 2 AutoResetEvents, set them when each Background worker finishes and wait for them all in the main method with a WaitHandle.

WaitHandle[] handles = new WaitHandle[] { new AutoResetEvent(false), new AutoResetEvent(false)};

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);

    WaitHandle.WaitAll(this.handles);
    doSomething();
}


private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
       doSomethingelsebk1();
       ((AutoResetEvent)this.handles[0]).Set();
}

private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
       doSomethingelsebk2();
       ((AutoResetEvent)this.handles[1]).Set();
}
JoanComasFdz
  • 2,911
  • 5
  • 34
  • 50
  • thanks for your reply,i mofidy code as you said,but get a error message:WaitAll for multiple handles on a STA thread is not supported. – user2147152 Mar 28 '13 at 15:26
  • Hi, you can have a look at: http://stackoverflow.com/questions/4192834/waitall-for-multiple-handles-on-a-sta-thread-is-not-supported – JoanComasFdz Apr 02 '13 at 09:04