In the below test code, I am expecting this outcome:
1, 2000
2, 4000
3, 6000
However the actual outcome is:
3, 6000
2, 4000
1, 2000
Moreover, i only see the outcome on screen after 6 seconds. Which means anything input that is competed is waiting and processed to the next stage.
How can i make the pipeline to spit out the result per input a soon as is completed?
public static void Run()
{
Func<int, string> fn = n =>
{
var sleep = n * 2000;
Thread.Sleep(sleep);
return n + ", " + sleep;
};
var opts = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 4
};
var transformBlock = new TransformBlock<int, string>(fn, opts);
var bufferBlock = new BufferBlock<string>(opts);
transformBlock.LinkTo(bufferBlock, new DataflowLinkOptions { PropagateCompletion = true });
for (var i = 3; i > 0; i--)
transformBlock.Post(i);
Console.WriteLine(bufferBlock.Receive());
Console.WriteLine(bufferBlock.Receive());
Console.WriteLine(bufferBlock.Receive());
}