有没有像异步BlockingCollection与其中的任何事情; T>? [英] Is there anything like asynchronous BlockingCollection<T>?

查看:115
本文介绍了有没有像异步BlockingCollection与其中的任何事情; T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想等待 BlockingCollection&LT的结果; T>。取()异步的,所以我不阻塞线程。寻找这样的事情:

I would like to await on the result of BlockingCollection<T>.Take() asynchronously, so I do not block the thread. Looking for anything like this:

var item = await blockingCollection.TakeAsync();

我知道我能做到这一点:

I know I could do this:

var item = await Task.Run(() => blockingCollection.Take());

但还挺杀死的整体思路,因为(对线程池)的另一个线程被阻塞,而不是。

but that kinda kills the whole idea, because another thread (of ThreadPool) gets blocked instead.

有没有什么别的选择吗?

Is there any alternative?

推荐答案

有三种选择,我知道了。

There are three alternatives that I know of.

首先是 BufferBlock&LT; T&GT; 从的 TPL数据流的。如果你只有一个消费者,你可以使用 OutputAvailableAsync ReceiveAsync ,或只是将它链接到 ActionBlock&LT; T&GT; 。欲了解更多信息,请看我的博客

The first is BufferBlock<T> from TPL Dataflow. If you only have a single consumer, you can use OutputAvailableAsync or ReceiveAsync, or just link it to an ActionBlock<T>. For more information, see my blog.

另外两个是我创建的类型,可在我的 AsyncEx库

The other two are types that I created, available in my AsyncEx library.

<一个href="http://nitoasyncex.$c$cplex.com/wikipage?title=AsyncCollection"><$c$c>AsyncCollection<T>异步近相当于 BlockingCollection&LT; T&GT; ,能够包裹并发生产者/消费者的收集,如 ConcurrentQueue&LT; T&GT; ConcurrentBag&LT; T&GT; 。您可以使用 TakeAsync 来异步使用的藏品。欲了解更多信息,请看我的博客

AsyncCollection<T> is the async near-equivalent of BlockingCollection<T>, capable of wrapping a concurrent producer/consumer collection such as ConcurrentQueue<T> or ConcurrentBag<T>. You can use TakeAsync to asynchronously consume items from the collection. For more information, see my blog.

<一个href="http://nitoasyncex.$c$cplex.com/wikipage?title=AsyncProducerConsumerQueue"><$c$c>AsyncProducerConsumerQueue<T>是一种更便携异步兼容的生产者/消费者队列。您可以使用 DequeueAsync 来异步使用从队列中的项目。欲了解更多信息,请看我的博客

AsyncProducerConsumerQueue<T> is a more portable async-compatible producer/consumer queue. You can use DequeueAsync to asynchronously consume items from the queue. For more information, see my blog.

这三种方案允许同步和异步看跌期权,并采取。

All three of these alternatives allow synchronous and asynchronous puts and takes.

这篇关于有没有像异步BlockingCollection与其中的任何事情; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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