title: C# once the main thread sleep, all thread stopped
body: <p>I have a class running the Producer-Consumer model like this:</p>
  
  <pre><code>public class SyncEvents
  {
      public bool waiting;
  
      public SyncEvents()
      {
          waiting = true;
      }
  }
  
  public class Producer
  {
      private readonly Queue&lt;Delegate&gt; _queue;
      private SyncEvents _sync;
      private Object _waitAck;
  
      public Producer(Queue&lt;Delegate&gt; q, SyncEvents sync, Object obj)
      {
          _queue = q;
          _sync = sync;
          _waitAck = obj;
      }
  
      public void ThreadRun()
      {
          lock (_sync)
          {
              while (true)
              {
                  Monitor.Wait(_sync, 0);
                  if (_queue.Count &gt; 0)
                  {
                      _sync.waiting = false;
                  }
                  else
                  {
                      _sync.waiting = true;
                      lock (_waitAck)
                      {
                          Monitor.Pulse(_waitAck);
                      }
                  }
                  Monitor.Pulse(_sync);
              }
          }
      }
  
  }
  
  public class Consumer
  {
      private readonly Queue&lt;Delegate&gt; _queue;
      private SyncEvents _sync;
  
      private int count = 0;
  
      public Consumer(Queue&lt;Delegate&gt; q, SyncEvents sync)
      {
          _queue = q;
          _sync = sync;
      }
  
      public void ThreadRun()
      {
          lock (_sync)
          {
              while (true)
              {
                  while (_queue.Count == 0)
                  {
                      Monitor.Wait(_sync);
                  }
  
                  Delegate query = _queue.Dequeue();
                  query.DynamicInvoke(null);
  
                  count++;
  
                  Monitor.Pulse(_sync);
              }
          }
      }
  }
  
  /// &lt;summary&gt;
  /// Act as a consumer to the queries produced by the DataGridViewCustomCell
  /// &lt;/summary&gt;
  public class QueryThread
  {
      private SyncEvents _syncEvents = new SyncEvents();
      private Object waitAck = new Object();
      private Queue&lt;Delegate&gt; _queryQueue = new Queue&lt;Delegate&gt;();
  
      Producer queryProducer;
      Consumer queryConsumer;
  
      public QueryThread()
      {
          queryProducer = new Producer(_queryQueue, _syncEvents, waitAck);
          queryConsumer = new Consumer(_queryQueue, _syncEvents);
  
          Thread producerThread = new Thread(queryProducer.ThreadRun);
          Thread consumerThread = new Thread(queryConsumer.ThreadRun);
  
          producerThread.IsBackground = true;
          consumerThread.IsBackground = true;
  
          producerThread.Start();
          consumerThread.Start();
      }
  
      public bool isQueueEmpty()
      {
          return _syncEvents.waiting;
      }
  
      public void wait()
      {
          lock (waitAck)
          {
              while (_queryQueue.Count &gt; 0)
              {
                  Monitor.Wait(waitAck);
              }
          }
      }
  
      public void Enqueue(Delegate item)
      {
          _queryQueue.Enqueue(item);
      }
  }
  </code></pre>
  
  <p>The code run smoothly but the wait() function.
  In some case I want to wait until all the function in the queue were finished running so I made the wait() function.</p>
  
  <p>The producer will fire the waitAck pulse at suitable time.</p>
  
  <p>However, when the line "Monitor.Wait(waitAck);" is ran in the wait() function, all thread stop, includeing the producer and consumer thread.</p>
  
  <p>Why would this happen and how can I solve it? thanks!</p>