static void Main(string[] args)
 {
     int maxParallelTasks = 4;
     int totalTasks = 10;
     BlockingCounter blockingCounter = new BlockingCounter(maxParallelTasks);
     List<ManualResetEvent> waitList = new List<ManualResetEvent>();
     for (int i = 1; i <= totalTasks; i++)
     {
         
         ManualResetEvent finishEvent = new ManualResetEvent(false);
         waitList.Add(finishEvent);
         Console.WriteLine("Submitting task {0}",i);
         blockingCounter.WaitableIncrement();
         if (!ThreadPool.QueueUserWorkItem((obj) =>
                                               {
                                                     try
                                                     {
                                                         ThreadProc(obj);
                                                     }
                                                     catch(Exception ex)
                                                     {
                                                         Console.Error.WriteLine("Task {0} failed: {1}", obj, ex.Message);
                                                     }
                                                     finally
                                                     {
                                                         // Exceptions are possible here too, 
                                                         // but proper error handling is not the goal of this sample
                                                         finishEvent.Set();
                                                         blockingCounter.WaitableDecrement();
                                                     }
                                               }, i))
         {
             waitList.Remove(finishEvent);
             blockingCounter.WaitableDecrement();
             Console.Error.WriteLine("Failed to submit task {0} for execution.", i);
         }
     }
     blockingCounter.Close();
     Console.WriteLine("Waiting for copmletion...");
     foreach (ManualResetEvent finishEvent in waitList)
     {
         finishEvent.WaitOne(10000); 
     }
     Console.WriteLine("Work done!");
     Console.ReadKey();
 }
Exemple #2
0
 static void Main(string[] args)
 {
     int maxParallelTasks = 4;
     int totalTasks = 10;
     using (BlockingCounter blockingCounter = new BlockingCounter(maxParallelTasks))
     {
         for (int i = 1; i <= totalTasks; i++)
         {
             Console.WriteLine("Submitting task {0}", i);
             blockingCounter.WaitableIncrement();
             if (!ThreadPool.QueueUserWorkItem((obj) =>
                                                   {
                                                       try
                                                       {
                                                           ThreadProc(obj);
                                                       }
                                                       catch (Exception ex)
                                                       {
                                                           Console.Error.WriteLine("Task {0} failed: {1}", obj, ex.Message);
                                                       }
                                                       finally
                                                       {
                                                           // Exceptions are possible here too, 
                                                           // but proper error handling is not the goal of this sample
                                                           blockingCounter.WaitableDecrement();
                                                       }
                                                   }, i))
             {
                 blockingCounter.WaitableDecrement();
                 Console.Error.WriteLine("Failed to submit task {0} for execution.", i);
             }
         }
         Console.WriteLine("Waiting for copmletion...");
         blockingCounter.CloseAndWait(30000);
     }
     Console.WriteLine("Work done!");
     Console.ReadKey();
 }