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

TransformManyBlockTInput TOutput

(SelectMany, 1-m: The results of this mapping are “flattened”, just like LINQ’s SelectMany)

TransformManyBlock is very similar to TransformBlock.

The key difference is that whereas a TransformBlock produces one and only one output for each input, TransformManyBlock produces any number (zero or more) outputs for each input. As with ActionBlock and TransformBlock, this processing may be specified using delegates, both for synchronous and asynchronous processing.

A Func is used for synchronous, and a Func> is used for asynchronous. As with both ActionBlock and TransformBlock, TransformManyBlock defaults to sequential processing, but may be configured otherwise.

The mapping delegate retuns a collection of items, which are inserted individually into the output buffer.

Asynchronous Web Crawler

var downloader = new TransformManyBlock<string, string>(async url =>
{
    Console.WriteLine(Downloading  + url);
    try 
    { 
        return ParseLinks(await DownloadContents(url)); 
    } 
    catch{}
    
    return Enumerable.Empty<string>();
});
downloader.LinkTo(downloader);

Expanding an Enumerable Into Its Constituent Elements

var expanded = new TransformManyBlock<T[], T>(array => array);

Filtering by going from 1 to 0 or 1 elements

public IPropagatorBlock<T> CreateFilteredBuffer<T>(Predicate<T> filter)
{
    return new TransformManyBlock<T, T>(item =>
        filter(item) ? new [] { item } : Enumerable.Empty<T>());
}

Introduction to TPL Dataflow by Stephen Toub


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