title: ConcurrentQueue and Parallel.ForEach
body: <p>I have a ConcurrentQueue with a list of URLs that I need to get the the source of. When using the Parallel.ForEach with the ConcurrentQueue object as the input parameter, the Pop method won't work nothing (Should return a string).</p>
  
  <p>I'm using Parallel with the MaxDegreeOfParallelism set to four. I really need to block the number of concurrent threads. Is using a queue with Parallelism redundant?</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>// On the main class
  var items = await engine.FetchPageWithNumberItems(result);
  // Enqueue List of items
  itemQueue.EnqueueList(items);
  var crawl = Task.Run(() =&gt; { engine.CrawlItems(itemQueue); });
  
  // On the Engine class
  public void CrawlItems(ItemQueue itemQueue)
  {
  Parallel.ForEach(
              itemQueue,
              new ParallelOptions {MaxDegreeOfParallelism = 4},
              item =&gt;
              {
  
                  var worker = new Worker();
                  // Pop doesn't return anything
                  worker.Url = itemQueue.Pop();
                  /* Some work */
               });
   }
  
  // Item Queue
  class ItemQueue : ConcurrentQueue&lt;string&gt;
      {
          private ConcurrentQueue&lt;string&gt; queue = new ConcurrentQueue&lt;string&gt;();
  
          public string Pop()
          {
              string value = String.Empty;
              if(this.queue.Count == 0)
                  throw new Exception();
              this.queue.TryDequeue(out value);
              return value;
          }
  
          public void Push(string item)
          {
              this.queue.Enqueue(item);
          }
  
          public void EnqueueList(List&lt;string&gt; list)
          {
              list.ForEach(this.queue.Enqueue);
          }
      }
  </code></pre>