【问题标题】:Linked block doesn't complete链接块未完成
【发布时间】:2014-01-02 00:53:02
【问题描述】:

我从 TPL 数据流开始。 我创建了以下工作代码。 readFilesBlock 是一个 BufferBlock 它是这样填充的:

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(); 

现在我像这样使用 BufferBlock (readFilesBlock)

while (await readFilesBlock.OutputAvailableAsync())
    {
        var file = await readFilesBlock.ReceiveAsync();

        ActionBlock<SourceCodeFile> action = new ActionBlock<SourceCodeFile>(item => storeResultsInBag(resultBag, item));
        await action.SendAsync(file);
    }

这很好用。 现在我想使用链接功能

我试过了:

var storeFilesInBagAction = new ActionBlock<SourceCodeFile>(item => storeResultsInBag(resultBag, item));

readFilesBlock.LinkTo(storeFilesInBagAction);

await storeFilesInBagAction.Completion;

但这一次我永远不会完成。

我做错了什么?

当我不等待 Bagaction 中的存储文件时,没有退回物品。

【问题讨论】:

  • 1.避免async void。 2.您不需要为每个文件创建新的ActionBlock

标签: .net task-parallel-library tpl-dataflow


【解决方案1】:

默认情况下,数据流块不会传播完成。这是设计使然;数据流可以表示任何类型的网格,包括拆分、连接和循环(不仅仅是管道)。

您可以在链接时设置一个PropagateCompletion option,它将传播完成。

readFilesBlock.LinkTo(storeFilesInBagAction, new DataflowLinkOptions
{
    PropagateCompletion = true,
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
相关资源
最近更新 更多