title: .NET BlockingCollection<T> CPU usage
body: <p>Running this program will chew up 25% CPU power in a four cores system. So basically something is running at full thrust. I narrowed it down to the consumer however the load doesn't stop on pressing "x" which should terminate my consumers.</p>
  
  <p><strong>my code</strong></p>
  
  <pre><code>internal class TestBlockingCollectionConsumerProducer2
  {
      private int _itemCount;
  
      internal void Run()
      {
          BlockingCollection&lt;string&gt; blockingCollection = new BlockingCollection&lt;string&gt;();
  
          // The token source for issuing the cancelation request.
          CancellationTokenSource cts = new CancellationTokenSource();
  
          // Simple thread waiting for a Console 'x'
          Task.Factory.StartNew(() =&gt;
          {
              if (Console.ReadKey().KeyChar == 'x')
              {
                  cts.Cancel();
              }
          });
  
          // start producer
          Task.Factory.StartNew(() =&gt; Produce(blockingCollection, cts.Token));
  
          // start multiple consumers
          const int THREAD_COUNT = 5;
          for (int i = 0; i &lt; THREAD_COUNT; i++)
          {
              Task.Factory.StartNew(() =&gt; Consume(blockingCollection, cts.Token));
          }
  
          while (true);
      }
  
      private void Produce(BlockingCollection&lt;string&gt; blockingCollection, CancellationToken cancellationToken)
      {
          while (true)
          {
              for (int i = 0; i &lt; 10; i++)
              {
                  blockingCollection.Add(string.Format("Item {0}", _itemCount++), cancellationToken);
              }
  
              Console.WriteLine("Added 10 items. Current queue length:" + blockingCollection.Count);
              Thread.Sleep(10000);
          }
      }
  
      private void Consume(BlockingCollection&lt;string&gt; blockingCollection, CancellationToken cancellationToken)
      {
          try
          {
              foreach (string item in blockingCollection.GetConsumingEnumerable(cancellationToken))
              {
                  Console.WriteLine(string.Format("[{0}] Consumer: Consuming: {1}", Thread.CurrentThread.ManagedThreadId, item));
                  Thread.Sleep(2500);
              }
          }
          catch (OperationCanceledException)
          {
              Console.WriteLine("[{0}] Consumer: Operation has been canceled.", Thread.CurrentThread.ManagedThreadId);
          }
      }
  }
  </code></pre>
  
  <blockquote>
    <p>My question is:<br>
    1. <strong>Why is the CPU load so high?</strong> Shouldn't GetConsumingEnumerable() block and therefore use no CPU time at all?<br>
    2. <strong>Why doesn't it stop on cts.Cancel()?</strong></p>
  </blockquote>