Is there a C# class like Queue that implements IAsyncEnumerable?

心不动则不痛 提交于 2020-03-22 15:45:35

问题


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?


回答1:


If you are using the .NET Core platform there are at least two built-in options:

  1. The System.Threading.Tasks.Dataflow.BufferBlock<T> class, part of the TPL Dataflow library. It doesn't implement the IAsyncEnumerable<T> natively, but it exposes the awaitable OutputAvailableAsync() method, doing it trivial to implement a ToAsyncEnumerable extension method.

  2. The System.Threading.Channels.Channel<T> class, the core component of the Channels library. It exposes an IAsyncEnumerable<T> implementation via its Reader.ReadAllAsync()¹ method.

Both classes are also available for .NET Framework, by installing a nuget package (different for each one).

An implementation of IAsyncEnumerable<T> for BufferBlock<T>:

public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(
    this IReceivableSourceBlock<T> source,
    [EnumeratorCancellation]CancellationToken cancellationToken = default)
{
    while (await source.OutputAvailableAsync(cancellationToken).ConfigureAwait(false))
    {
        while (source.TryReceive(out T item))
        {
            yield return item;
        }
    }
}

¹ (not available for NET Framework, but easy to implement in a similar way)



来源:https://stackoverflow.com/questions/58484958/is-there-a-c-sharp-class-like-queue-that-implements-iasyncenumerable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!