Both Queue
and ConcurrentQueue
implement IEnumerable
but not IAsyncEnumerable
. Is there a standard class or class available on NuGet which implements IAsyncEnumerable
such that, if the queue is empty, the result of MoveNextAsync
does not complete until something next is added to the queue?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
If you are using the .NET Core platform there are at least two built-in options:
The
System.Threading.Tasks.Dataflow.BufferBlock<T>
class, part of the TPL Dataflow library. It doesn't implement theIAsyncEnumerable<T>
natively, but it exposes the awaitableOutputAvailableAsync()
method, doing it trivial to implement aToAsyncEnumerable
extension method.The
System.Threading.Channels.Channel<T>
class, the core component of the Channels library. It exposes anIAsyncEnumerable<T>
implementation via itsReader.ReadAllAsync()
¹ method.Both classes are also available for .NET Framework, by installing a nuget package (different for each one).
An implementation of
IAsyncEnumerable<T>
forBufferBlock<T>
:¹ (not available for NET Framework, but easy to implement in a similar way)