Search code examples
c#backgroundworker.net-4.5

Synchronize background worker


I have a code like this:

if (condition#1) {
   // step 2
   ConfigurazioneSA csa = new ConfigurazioneSA(...);
   WconfiguraSA.RunWorkerAsync(csa);
}
else
{
   // step 1
   PassaggioIE bo = new PassaggioIE(...);
   WpassIE.RunWorkerAsync(bo);

   if (condition#2) {
      // step 2
      ConfigurazioneSA csa = new ConfigurazioneSA(...);
      WconfiguraSA.RunWorkerAsync(csa);
   }
}

When execution flow is inside condition#1 branch, the step 1 block has been previously executed (in a previous execution of my application) so step 2 can be executed without any problem.

But, when execution flow is inside else branch I need to execute step 1 and when this step is completed I can execute step 2. Since I'm using BackgroundWorker as step 1 start, step 2starts immediately after causing error.

I would sinchronize this producer/consumer problem adding as less entropy as I can. All solutions that I found introduce a great amount of code, while I would a simply semaphore that avoid the step 2 execution until step 1 is not complete. Any ideas?


Solution

  • IsBusy example:

    else
    {
       // step 1
       PassaggioIE bo = new PassaggioIE(...);
       WpassIE.RunWorkerAsync(bo);
    
       // wait for WpassIE to complete
       while(WpassIE.IsBusy) Application.DoEvents();
    
       if (condition#2) {
          // step 2
          ConfigurazioneSA csa = new ConfigurazioneSA(...);
          WconfiguraSA.RunWorkerAsync(csa);
       }
    }
    

    In this solution, you've to wait WpassIE completion.

    and don't use Thread.Sleep instead of Application.DoEvents because it causes problem like BackgroundWorkers never stop being busy.