Skip to content

somdoron/AsyncCollection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages