title: Limiting number of threads when dequeuing from a BlockingCollection
body: <p>I am currently using this to limit the number of processing threads:</p>
  <pre><code>Dim ListOfFiles New List(of String) From {...}
  Dim parallelOptions As ParallelOptions = New ParallelOptions With {.MaxDegreeOfParallelism = 6}
  Parallel.ForEach(ListOfFiles, parallelOptions, Sub(F)
                                                      'Process one file
                                                 End Sub)
  </code></pre>
  <p>which works well because I can control the number of concurrent threads to suit other processing going on simultaneously.</p>
  <p>But I need to keep adding items to the list. Instead of a fixed size list, I want to use a <code>ConcurrentQueue</code> so that I can continue enqueuing a list of files (from a Producer thread) while the parallel Consumer processing is going on. Clearly <code>Parallel.ForEach</code> wont work. I could use a <code>BlockingCollection</code> but I cant determine how best to only dequeue items as threads become available and block when they are all busy. Sitting in a loop and counting threads seems ugly.</p>
  <p>Any thoughts would be welcome.</p>