var squareBlock = new TransformBlock(x => x * x); // Send input to the block for (int i = 0; i < 10; i++) { squareBlock.SendAsync(i); } // Signal the end of input squareBlock.Complete(); // Receive output from the block for (int i = 0; i < 10; i++) { Console.WriteLine(await squareBlock.ReceiveAsync()); }
var batchBlock = new BatchBlockBoth examples use the System.Threading.Tasks.Dataflow package library.(3); // Send input to the block for (int i = 0; i < 10; i++) { batchBlock.SendAsync(i); } // Signal the end of input batchBlock.Complete(); // Receive output from the block List batches = new List (); while (await batchBlock.OutputAvailableAsync()) { batches.Add(await batchBlock.ReceiveAsync()); } foreach (var batch in batches) { Console.WriteLine($"Batch size: {batch.Length}, values: {string.Join(", ", batch)}"); }