title: Synchronization Exception
body: <p>I have two threads, one thread processes a queue and the other thread adds stuff into the queue.</p>
  
  <ol>
  <li>I want to put the queue processing thread to sleep when its finished processing the queue</li>
  <li>I want to have the 2nd thread tell it to wake up when it has added an item to the queue</li>
  </ol>
  
  <p>However these functions call <code>System.Threading.SynchronizationLockException: Object synchronization method was called from an unsynchronized block of code</code> on the <code>Monitor.PulseAll(waiting);</code> call, because I havent synchronized the function with the waiting object. [which I dont want to do, i want to be able to process while adding items to the queue]. How can I achieve this?</p>
  
  <pre><code>Queue&lt;object&gt; items = new Queue&lt;object&gt;();
  object waiting = new object();
  </code></pre>
  
  <p>1st Thread</p>
  
  <pre><code>public void ProcessQueue()
  {
   while (true)
   {
     if (items.Count == 0)
       Monitor.Wait(waiting);
  
      object real = null;
      lock(items) {
      object item = items.Dequeue();
      real = item;
      }
      if(real == null)
          continue;
      .. bla bla bla
   } 
  }
  </code></pre>
  
  <p>2nd Thread involves</p>
  
  <pre><code>public void AddItem(object o)
  {
   ... bla bla bla
   lock(items)
   {
   items.Enqueue(o);
   }
   Monitor.PulseAll(waiting);
  }
  </code></pre>