I have added a simple class CountedEvent to your code [1]
Now you do not have to burn the cpu waiting for all jobs to finish.

Its interesting to see that RunWorkerCompleted is called after(!) all
jobs have finished... This is due to that BackgroundWorker is designed
to work in an UI environment. When no real UI response is needed the
BackgroundWorker would not be my first choice for this kind of work. I
supports Gregs suggestion to use QueueUserWorkItem, and perhaps in
combination with CountedEvent.

HTH
// Ryan

[1]
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Collections;

namespace BackgroundWorkerDemo
{
   class CountedEvent
       {
         ManualResetEvent evt = new ManualResetEvent(true);
         int calls = 0;

         public void WaitOne()
         {
           evt.WaitOne();
         }
         public void Set()
         {
           lock( evt)
               {
                 calls--;
             if ( calls == 0)
                 {
                   evt.Set();
                 }
               }
         }
         public void Reset()
         {
               lock( evt)
               {
                 if ( calls == 0)
                 {
                   evt.Reset();
                 }
                 calls++;
               }
         }
       }

  class Program
  {
      static void Main(string[] args)
      {
          DateTime start_time = DateTime.Now;

          Console.WriteLine("Program starting");

          //RunSyncVersion();
          RunAsyncVersion();

          DateTime end_time = DateTime.Now;

          TimeSpan ts = end_time - start_time;

          Console.WriteLine("Program completed. Duration={0}
seconds.", ts.Seconds);
          Console.Read();
      }

      //Synchronous version
      static void RunSyncVersion()
      {
          for (int i = 0; i <= 10; i++)
          {
              LongRunningOperation(i);
          }
      }

      //This version uses the async version
      static void RunAsyncVersion()
      {
                       CountedEvent jobFinished = new CountedEvent();
          ArrayList lstBGWs = new ArrayList();

          for (int i = 0; i <= 10; i++)
          {
              BackgroundWorker worker = new BackgroundWorker();
                               jobFinished.Reset();

              worker.DoWork += new DoWorkEventHandler(worker_DoWork);

              worker.RunWorkerCompleted += delegate(object sender,
RunWorkerCompletedEventArgs e)
                       {
                                       jobFinished.Set();
                       Console.WriteLine("Set called");
                       };

              lstBGWs.Add(worker);

              worker.RunWorkerAsync(i);
          }

                       jobFinished.WaitOne();
                       Console.WriteLine("exit");
      }

      static void worker_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = sender as BackgroundWorker;

          LongRunningOperation(Convert.ToInt32(e.Argument));
      }

      //Long running operation
      private static void LongRunningOperation(int operation_number)
      {
          Console.WriteLine("Operation# {0} starting on Thread {1}",
operation_number, Thread.CurrentThread.ManagedThreadId);
          for (int i=0; i < 1000000000; i++)
          {

          }

          Console.WriteLine("Finished operation# {0} on Thread {1}",
operation_number, Thread.CurrentThread.ManagedThreadId);

      }
  }
}

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to