Search code examples
c#task-parallel-librarypipelinetpl-dataflow

C# TPL Dataflow - Completion not working


This code never reaches the last line because the completion doesn't propagate from the saveBlock to the sendBlock. What am I doing wrong?

var readGenerateBlock = new TransformBlock<int, int>(n =>
    {
        Console.WriteLine("Read " + n);
        Thread.Sleep(15);
        return n;
    }); 
var groupingBlock = new BatchBlock<int>(10);
var saveBlock = new TransformManyBlock<int[], int>(n =>
    {
        Console.WriteLine("Saving {0} items [{1}; {2}]", n.Count(), n.First(), n.Last());
        Thread.Sleep(150);
        return n;
    }); 
var sendBlock = new TransformBlock<int, int>(n =>
    {
        Console.WriteLine("Sending {0}", n);
        Thread.Sleep(25);
        return n;
    }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 }); 

readGenerateBlock.LinkTo(groupingBlock, new DataflowLinkOptions { PropagateCompletion = true });
groupingBlock.LinkTo(saveBlock, new DataflowLinkOptions { PropagateCompletion = true });
saveBlock.LinkTo(sendBlock, new DataflowLinkOptions { PropagateCompletion = true });

Parallel.For(0, 250, i => readGenerateBlock.Post(i));
readGenerateBlock.Complete();

sendBlock.Completion.Wait();
Console.WriteLine("Completed.");

Solution

  • You have to read the data out of the block before it will be completed. Since noöne is reading saveBlock, it will never be completed.

    If you don't need the data, the easiest solution is to use ActionBlock instead of TransformBlock. Otherwise, just keep reading the data until the block is completed.