title: .NET Threadpool synchronizing using AutoResetEvent
body: <p>Following is the code that I use. The main thread waits for Threadpool threads to execute. I use AutoResetEvent (WaitHandle), but I am really surprised that I am way off the mark, as the code doesn't behave as expected.</p>
  
  <p>I have two concentric for loops, where Threadpool is in the inner loop and it is expected that for each iteration of the outer loop, all the inner loop values should be processed. Main thread is made to wait using AutoResetEvent WaitOne call just outside inner loop, a static variable which is reset on every iteration of the outerloop to the max value of the inner loop, and is decremented using Interlock in the method call on Threadpool thread is used to call the Set for the AutoResetEvent. However, even when I expect the static variable to show the value 0 after every inner loop, it doesn't. What is the issue in my code and what are better options for me to accomplish the task? In fact, due to the mix up of the values, the main thread doesn't really seem to be waiting for the Threadpool threads.</p>
  
  <pre><code>using System;
  using System.Threading;
  
  namespace TestThreads
  {
  class Program
  {
      private static int threadingCounter = 0;
      private static readonly object lockThreads = new Object();
      private AutoResetEvent areSync = new AutoResetEvent(true);
  
      // &lt;param name="args"&gt;&lt;/param&gt;
      static void Main(string[] args)
      {
          Program myProgram = new Program();
  
          try
          {
              try
              {   
                  for (int outer = 0; outer &lt; 1000; outer++)
                  {
                      threadingCounter = 500;
                      try
                      {
                          for (int inner = 0; inner &lt; 500; inner++)
                          {
                              ThreadPool.QueueUserWorkItem(new
                                   WaitCallback(myProgram.ThreadCall), inner);
                          }
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine("Exception :: " + ex.Message);
                      }
                      finally
                      {                            
                          myProgram.areSync.WaitOne();
                      }
  
                      if(threadingCounter != 0)
                          Console.WriteLine("In Loop1, Thread Counter :: " +
                              threadingCounter);
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Exception :: " + ex.Message);
              }                
          }
          catch(Exception ex)
          {
              Console.WriteLine("Exception :: " + ex.Message);
          }
          finally
          {
              threadingCounter = 0;
  
              if (myProgram.areSync != null)
              {
                  myProgram.areSync.Dispose();
                  myProgram.areSync = null;
              }
          }
      }
  
      public void ThreadCall(object state)
      {
          try
          {
              int inner = (int)state;
              Thread.Sleep(1);
          }
          catch (Exception ex)
          {
              Console.WriteLine("Exception :: " + ex.Message);
          }
          finally
          {
              Interlocked.Decrement(ref threadingCounter);
              if (threadingCounter &lt;= 0)
                  areSync.Set();
          }                
      }        
   }
  }
  </code></pre>