How to empty a BlockingCollection

前端 未结 6 2246
闹比i
闹比i 2021-02-18 21:34

I have a thread adding items to a BlockingCollection .

On another thread I am using foreach (var item in myCollection.GetConsumingEnumerable())

6条回答
  •  无人共我
    2021-02-18 22:02

    This worked for me

    while (bCollection.Count > 0)
    {
        var obj = bCollection.Take();
        obj.Dispose();
    }
    

    Take() removes from the collection and you can call any clean up on your object and the loop condition does not invoke any blocking calls.

提交回复
热议问题