Boa*_*ler 1 .net task-parallel-library tpl-dataflow
我从TPL DataFlow开始.我创建了以下工作代码.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();
Run Code Online (Sandbox Code Playgroud)
现在我像这样使用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);
}
Run Code Online (Sandbox Code Playgroud)
这很好.现在我想使用链接功能
我试过了:
var storeFilesInBagAction = new ActionBlock<SourceCodeFile>(item => storeResultsInBag(resultBag, item));
readFilesBlock.LinkTo(storeFilesInBagAction);
await storeFilesInBagAction.Completion;
Run Code Online (Sandbox Code Playgroud)
但这次我永远不会完成.
我究竟做错了什么?
当我没有等待Bagaction的商店文件时,没有退回物品.
默认情况下,数据流块不会传播完成.这是设计的; 数据流可以表示任何类型的网格,包括拆分,连接和循环(不仅仅是管道).
有一个PropagateCompletion选项,你可以链接,将传播完成时设置.
readFilesBlock.LinkTo(storeFilesInBagAction, new DataflowLinkOptions
{
PropagateCompletion = true,
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99 次 |
| 最近记录: |