BlockingCollection.IsComplete 永远不会是真的

BlockingCollection.IsComplete is never true

.NET BlockingCollection class 对我来说效果很好,但出于某种原因 IsCompleted 属性 总是 returns false ... 即使 BlockingCollection.Count 是0.

while (!WorkUnits.IsCompleted)
{
   ... 
   // WorkUnits.Count == 0 but IsCompleted stays false
}

作为解决方法,我可以通过这种方式跳出外部 while(!blockingCollection.IsComplete) 循环:

if (WorkUnits.Count == 0 )
{
    break;
}

这有效,但增加了一些看起来应该已经有效的东西的膨胀,但我怀疑我可能对 IsComplete 的理解不够透彻,无法正确利用它。

编辑:

谢谢迈克和斯科特!我采纳了您的建议并创建了一个新的 public GitHub 项目 GhostLine.

如果您阅读 BlockingCollection (https://msdn.microsoft.com/en-us/library/dd267312(v=vs.110).aspx) 的文档,您会发现以下信息:

A producing thread can call the CompleteAdding method to indicate that no more items will be added. Consumers monitor the IsCompleted property to know when the collection is empty and no more items will be added.

那么,你曾经调用过 CompleteAdding 吗?