【发布时间】: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