title: How to correctly use BlockingCollection.GetConsumingEnumerable?
body: <p>I'm trying to implement a producer/consumer pattern using <code>BlockingCollection&lt;T&gt;</code> so I've written up a simple console application to test it.</p>
  
  <pre><code>public class Program
  {
      public static void Main(string[] args)
      {
          var workQueue = new WorkQueue();
          workQueue.StartProducingItems();
          workQueue.StartProcessingItems();
  
          while (true)
          {
  
          }
      }
  }
  
  public class WorkQueue
  {
      private BlockingCollection&lt;int&gt; _queue;
      private static Random _random = new Random();
  
      public WorkQueue()
      {
          _queue = new BlockingCollection&lt;int&gt;();
  
          // Prefill some items.
          for (int i = 0; i &lt; 100; i++)
          {
              //_queue.Add(_random.Next());
          }
      }
  
      public void StartProducingItems()
      {
          Task.Run(() =&gt;
          {
              _queue.Add(_random.Next()); // Should be adding items to the queue constantly, but instead adds one and then nothing else.
          });
      }
  
      public void StartProcessingItems()
      {
          Task.Run(() =&gt;
          {
              foreach (var item in _queue.GetConsumingEnumerable())
              {
                  Console.WriteLine("Worker 1: " + item);
              }
          });
  
          Task.Run(() =&gt;
          {
              foreach (var item in _queue.GetConsumingEnumerable())
              {
                  Console.WriteLine("Worker 2: " + item);
              }
          });
      }
  }
  </code></pre>
  
  <p>However there are 3 problems with my design:</p>
  
  <ul>
  <li><p>I don't know the correct way of blocking/waiting in my Main method. Doing a simple empty while loop seems terribly inefficient and CPU usage wasting simply for the sake of making sure the application doesn't end.</p></li>
  <li><p>There's also another problem with my design, in this simple application I have a producer that produces items indefinitely, and should never stop. In a real world setup, I'd want it to end eventually (e.g. ran out of files to process). In that case, how should I wait for it to finish in the Main method? Make <code>StartProducingItems</code> <code>async</code> and then <code>await</code> it?</p></li>
  <li><p>Either the <code>GetConsumingEnumerable</code> or <code>Add</code> is not working as I expected. The producer should constantly adding items, but it adds one item and then never adds anymore. This one item is then processed by one of the consumers. Both consumers then block waiting for items to be added, but none are. I know of the <code>Take</code> method, but again spinning on <code>Take</code> in a while loop seems pretty wasteful and inefficient. There is a <code>CompleteAdding</code> method but that then does not allow anything else to ever be added and throws an exception if you try, so that is not suitable.</p></li>
  </ul>
  
  <p>I know for sure that both consumers are in fact blocking and waiting for new items, as I can switch between threads during debugging:</p>
  
  <p><a href="https://i.stack.imgur.com/2QARa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2QARa.png" alt="enter image description here"></a></p>
  
  <p>EDIT:</p>
  
  <p>I've made the changes suggested in one of the comments, but the <code>Task.WhenAll</code> still returns right away.</p>
  
  <pre><code>public Task StartProcessingItems()
  {
      var consumers = new List&lt;Task&gt;();
  
      for (int i = 0; i &lt; 2; i++)
      {
          consumers.Add(Task.Run(() =&gt;
          {
              foreach (var item in _queue.GetConsumingEnumerable())
              {
                  Console.WriteLine($"Worker {i}: " + item);
              }
          }));
      }
  
      return Task.WhenAll(consumers.ToList());
  }
  </code></pre>