如何清空BlockingCollection [英] How to empty a BlockingCollection

查看:2483
本文介绍了如何清空BlockingCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程添加项目到 BlockingCollection

I have a thread adding items to a BlockingCollection .

在另一个线程我使用 的foreach(在myCollection.GetConsumingEnumerable VAR项目())

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

如果有问题,我想打破我的foreach,我的方法,并明确无论是留在 BlockingCollection 不过,我无法找到一个方法来做到吧。

If there is a problem I want to break out of my foreach and my method and clear whatever is left in the BlockingCollection however I can't find a way to do it.

任何想法?

推荐答案

可能使用 GetConsumingEnumerable 的重载这需要一个的CancellationToken ,然后如果有什么不顺心的生产方,也可以取消消费者。

Possibly use the overload of GetConsumingEnumerable which takes a CancellationToken, and then if anything goes wrong from the producing side, it can cancel the consumer.

这篇关于如何清空BlockingCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆