title: How to wait for a thread to finish execution in C#?
body: <p>I have a function that is called in rapid succession that has a open database connection. 
  my issue is that before one database connection is closed, another instance of the function is called and i could possibly receive a deadlock in the database.</p>
  
  <p>I have tried:</p>
  
  <pre><code>private static WaitHandle[] waitHandles = new WaitHandle[]
      {
          new AutoResetEvent(false)
      };
  
  protected override void Broadcast(Data data, string updatedBy)
      {
          Action newAction = new Action(() =&gt;
          {
          DataManagerFactory.PerformWithDataManager( 
              dataManager =&gt;
              {
                  // Update status and broadcast the changes
                  data.UpdateModifiedColumns(dataManager, updatedBy);
  
                  BroadcastManager.Instance().PerformBroadcast(
                      data,
                      BroadcastAction.Update,
                      Feature.None);
              },
              e =&gt; m_log.Error(ServerLog.ConfigIdlingRequestHandler_UpdateFailed() + e.Message));
              }
          );
  
          Thread workerThread = new Thread(new ThreadStart(newAction));
          ThreadPool.QueueUserWorkItem(workerThread.Start, waitHandles[0]);
          WaitHandle.WaitAll(waitHandles);
      }
  </code></pre>
  
  <p>but i recieve a thread error and the program freezes.  It has something to do with the thread start function having no parameters i believe.</p>
  
  <p>Thanks for any help</p>