title: Is there a C# equivalent to Java's BlockingQueue.drainTo(Collection) method?
body: <p>I am building a multithreaded C# application in which multiple threads contribute to elements on a queue. A single thread is consuming the elements on that same queue. I would like the single thread to do some reduction/merging of elements of the incoming elements, so ideally it would look at all new elements on the queue, reduce them and then process the entries after reduction. A bit like this:</p>
  
  <pre><code>while (true)
  {
    Collection&lt;Elem&gt; elements = queue.TakeAll();
    Collection&lt;Elem&gt; reducedElements = Reduce(elements);
    for (Elem e in reducedElements)
    {
      process(e);
    }
  }
  </code></pre>
  
  <p>But there obviously isn't any TakeAll() method. From Java experience I am used to the <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#drainTo(java.util.Collection)" rel="nofollow noreferrer">BlockingQueue's drainTo method</a> which offers something like what I'm interested in.</p>
  
  <p>I could implement something myself by just using TryTake until the queue is empty. But that has the risk that the producing threads may potentially be busy producing also and that would cause there to be no finite ending of the collection to reduce and process. I'm basically looking for a way to take everything out of the queue, leaving it empty but providing a collection that can be worked on.</p>