Essential C# Task Parallel Library TPL Dataflow Constructs  Suggest an edit

BatchedJoinBlockT1 T2

(Collects a certain number of total items from 2-3 inputs and groups them into a Tuple of collections of data items)

BatchedJoinBlock is in a sense a combination of BatchBlock and JoinBlock.

Whereas JoinBlock is used to aggregate one input from each target into a tuple, and BatchBlock is used to aggregate N inputs into a collection, BatchedJoinBlock is used to gather N inputs from across all of the targets into tuples of collections.


Consider a scatter/gather problem where N operations are launched, some of which may succeed and produce string outputs, and others of which may fail and produce Exceptions.

var batchedJoin = new BatchedJoinBlock<string, Exception>(10);

for (int i=0; i<10; i++)
    Task.Factory.StartNew(() => {
        try { batchedJoin.Target1.Post(DoWork()); }
        catch(Exception ex) { batchJoin.Target2.Post(ex); }

var results = await batchedJoin.ReceiveAsync();

foreach(string s in results.Item1) 

foreach(Exception e in results.Item2) 

Introduction to TPL Dataflow by Stephen Toub

Table Of Contents
22 Enum
107 Stream
108 Timers
119 Task Parallel Library TPL Dataflow Constructs
  ↑ ↓ to navigate     ↵ to select     Esc to close