Skip to content

Jac21/BlockingCollectionExtensions

Repository files navigation

logo

NuGet Status MIT Licence CI donate

Helpers and adapters for working with BlockingCollection<T> in modern .NET applications.

Installation

Find it on nuget!

PM> Install-Package BlockingCollectionExtensions -Version 7.0.0

What's New

  • Simpler IProducerConsumerCollection<T> adapters with better defaults
  • TimeSpan-based overloads and optional cancellation tokens
  • IAsyncEnumerable<T> support for async-first producer pipelines
  • Modernized package metadata, symbols, readme packaging, and CI

Usage

Add from an enumerable

using System.Collections.Concurrent;
using BlockingCollectionExtensions;

var collection = new BlockingCollection<int>();
collection.AddFromEnumerable(new[] { 1, 2, 3 }, completeAddingWhenDone: true);

Add from an async enumerable

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;
}

Adapt to IProducerConsumerCollection<T>

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);

Target Frameworks

  • net8.0
  • netstandard2.1

Notes

  • 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.Channels may still be the better default choice.

About

๐Ÿ“Ž Utilities to aid in utilizing the ever-useful .NET structure BlockingCollection<T>

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages