什么时候BlockingCollection TryTake()可以返回false?

假设BlockingCollection正在使用下面的ConcurrentQueue,如果你使用Timeout.Infinite ,TryTake(T,Int32)方法什么时候会返回false?

这是一个简单的示例,显示何时可以返回false:当集合标记为CompleteAdding并变为空时

 //by default, BlockingCollection will use ConcurrentQueue BlockingCollection coll = new BlockingCollection(); coll.Add(1); coll.Add(2); coll.CompleteAdding(); int item; if (coll.TryTake(out item, -1)) { Console.WriteLine(item); } if (coll.TryTake(out item, -1)) { Console.WriteLine(item); } if (coll.TryTake(out item, -1)) { //this won't get hit } else { Console.WriteLine("TryTake returned false!"); } 

这允许您禁止在队列中添加新项目并完成剩余元素的处理

这将打印false

  var coll = new BlockingCollection(); coll.CompleteAdding(); // closed for business int v; bool result = coll.TryTake(out v, Timeout.Infinite); Console.WriteLine(result); 

所以基本上BlockingCollection支持2个独立的概念:Empty和Closed。 虽然TryTake()可以在Empty队列上永远等待,但当队列为Empty Closed时,它将返回false