I'm starting with TPL DataFlow. I've the following working code created. the readFilesBlock is a BufferBlock It is filled like this:
public async void ReadItems(ITargetBlock<SourceCodeFile> target)
{
foreach(var item in Source)
{
await target.SendAsync(item); //when To use post instead of sendasync?
}
}
target.Complete();
Now I cosume the BufferBlock (readFilesBlock) like this
while (await readFilesBlock.OutputAvailableAsync())
{
var file = await readFilesBlock.ReceiveAsync();
ActionBlock<SourceCodeFile> action = new ActionBlock<SourceCodeFile>(item => storeResultsInBag(resultBag, item));
await action.SendAsync(file);
}
This Works fine. Now I want to use the link Feature
I tried:
var storeFilesInBagAction = new ActionBlock<SourceCodeFile>(item => storeResultsInBag(resultBag, item));
readFilesBlock.LinkTo(storeFilesInBagAction);
await storeFilesInBagAction.Completion;
But this time i'll never get a completion.
What am I doing wrong?
When I'm not await the storefiles in Bagaction ,not items were returned.
By default, dataflow blocks do not propagate completion. This is by design; dataflows can represent any kind of mesh, including splits, joins, and loops (not just pipelines).
There is a PropagateCompletion
option you can set when linking that will propagate completion.
readFilesBlock.LinkTo(storeFilesInBagAction, new DataflowLinkOptions
{
PropagateCompletion = true,
});