using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
public static async Task Main()
var computer = new ComplicatedComputation(10);
Console.WriteLine("Result={0}", await computer.ComputeAsync());
public class ComplicatedComputation
private readonly int _input;
private double _result = 0;
private ITargetBlock<int> _startBlock;
private IDataflowBlock _endBlock;
public ComplicatedComputation(int input)
var fanOutBlock = new TransformManyBlock<int, int>(x =>
return Enumerable.Range(1, x).Select(x => x);
var squareBlock = new TransformBlock<int, int>(async x =>
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5 });
var addFiveBlock = new TransformBlock<int, int>(x =>
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5 });
var divTwoBlock = new TransformBlock<int, double>(x =>
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5 });
var sumBlock = new ActionBlock<double>(x =>
var options = new DataflowLinkOptions { PropagateCompletion = true };
fanOutBlock.LinkTo(squareBlock, options);
squareBlock.LinkTo(addFiveBlock, options);
addFiveBlock.LinkTo(divTwoBlock, options);
divTwoBlock.LinkTo(sumBlock, options);
_startBlock = fanOutBlock;
public async Task<double> ComputeAsync()
_startBlock.Post(_input);
await _endBlock.Completion;