Helpers and adapters for working with BlockingCollection<T> in modern .NET applications.
Find it on nuget!
PM> Install-Package BlockingCollectionExtensions -Version 7.0.0- Simpler
IProducerConsumerCollection<T>adapters with better defaults TimeSpan-based overloads and optional cancellation tokensIAsyncEnumerable<T>support for async-first producer pipelines- Modernized package metadata, symbols, readme packaging, and CI
using System.Collections.Concurrent;
using BlockingCollectionExtensions;
var collection = new BlockingCollection<int>();
collection.AddFromEnumerable(new[] { 1, 2, 3 }, completeAddingWhenDone: true);using System.Collections.Concurrent;
using BlockingCollectionExtensions;
var collection = new BlockingCollection<int>();
await collection.AddFromAsyncEnumerable(GetItemsAsync(), completeAddingWhenDone: true);
static async IAsyncEnumerable<int> GetItemsAsync()
{
yield return 1;
await Task.Yield();
yield return 2;
}using System.Collections.Concurrent;
using BlockingCollectionExtensions;
var collection = new BlockingCollection<int>();
var adapter = collection.AsProducerConsumerCollection(TimeSpan.FromMilliseconds(250));
adapter.TryAdd(42);
adapter.TryTake(out var value);net8.0netstandard2.1
BlockingCollection<T>is still useful for some producer/consumer scenarios, especially when interop with older code matters.- For brand new async-heavy pipelines,
System.Threading.Channelsmay still be the better default choice.
