Skip to content

Instantly share code, notes, and snippets.

@wullemsb
Created February 1, 2026 12:39
Show Gist options
  • Select an option

  • Save wullemsb/3e3445cf1ba4d86caa81f5e61ea663a9 to your computer and use it in GitHub Desktop.

Select an option

Save wullemsb/3e3445cf1ba4d86caa81f5e61ea663a9 to your computer and use it in GitHub Desktop.
using System.Threading.Tasks.Dataflow;
var broadcastBlock = new BroadcastBlock<int>(null);
var evenBlock = new ActionBlock<int>(n =>
Console.WriteLine($"Even: {n}"));
var oddBlock = new ActionBlock<int>(n =>
Console.WriteLine($"Odd: {n}"));
// Link with predicates
broadcastBlock.LinkTo(evenBlock, new DataflowLinkOptions { PropagateCompletion = true },
n => n % 2 == 0);
broadcastBlock.LinkTo(oddBlock, new DataflowLinkOptions { PropagateCompletion = true },
n => n % 2 != 0);
// Post some numbers
for (int i = 0; i < 10; i++)
{
broadcastBlock.Post(i);
}
broadcastBlock.Complete();
await Task.WhenAll(evenBlock.Completion, oddBlock.Completion);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment