BatchedJoinBlock C# with Example



BatchedJoinBlock C# with Example

(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. 
Scatter/Gather 
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(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) 
{ 
Console.WriteLine(s); 
} 
foreach(Exception e in results.Item2) 
{ 
Console.WriteLine(e); 
} 
Introduction to TPL Dataflow by Stephen Toub 
 

0 Comment's

Comment Form

Submit Comment