BatchBlock C# with Example
(Groups a certain number of sequential data items into collections of data items) BatchBlock combines N single items into one batch item, represented as an array of elements. An instance is created with a specific batch size, and the block then creates a batch as soon as it ’s received that number of elements, asynchronously outputting the batch to the output buffer. BatchBlock is capable of executing in both greedy and non-greedy modes. In the default greedy mode, all messages offered to the block from any number of sources are accepted and buffered to be converted into batches. In non-greedy mode, all messages are postponed from sources until enough sources have offered messages to the block to create a batch. Thus, a BatchBlock can be used to receive 1 element from each of N sources, N elements from 1 source, and a myriad of options in between. Batching Requests into groups of 100 to Submit to a Database var batchRequests = new BatchBlock(batchSize:100); var sendToDb = new ActionBlock(reqs => SubmitToDatabase(reqs)); batchRequests.LinkTo(sendToDb); Creating a batch once a second var batch = new BatchBlock(batchSize:Int32.MaxValue); new Timer(() => { batch.TriggerBatch(); }).Change(1000, 1000); Introduction to TPL Dataflow by Stephen Toub