somdoron / AsyncCollection

.Net Asynchronous BlockingCollection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AsyncCollection

AsyncCollection is an unbounded queue which you can asynchronously (using await) dequeue items from the queue.

AsyncCollection also implement C# 8.0 IAsyncEnumerable, which allow asynchronously iterating the queue using await foreach.

API of the colleciton is very similar to BlockingCollection.

Download

Install using nuget: https://www.nuget.org/packages/AsyncCollection/.

Example

Iterating the collection asynchronously:

AsyncCollection<int> collection = new AsyncCollection<int>();

var t = Task.Run(async () =>
{
    while (!collection.IsCompleted)
    {
        var item = await collection.TakeAsync();
        
        // process
    }
});

for (int i = 0; i < 1000; i++)
{
    collection.Add(i);
}

collection.CompleteAdding();

t.Wait();

Iterating the collection asynchronously using IAsyncEnumerable, require dotnet core 3.0:

AsyncCollection<int> collection = new AsyncCollection<int>();

var t = Task.Run(async () =>
{
    await foreach (var item in collection)
    {
        // process
    }
});

for (int i = 0; i < 1000; i++)
{
    collection.Add(i);
}

collection.CompleteAdding();

t.Wait();

About

.Net Asynchronous BlockingCollection

License:Mozilla Public License 2.0


Languages

Language:C# 100.0%