我正在使用 TPL 数据流构建一个应用程序。实际上我有以下问题。我有一个变换块var tfb1 = new TranformBlock<InMsg, IReadOnlyCollection<OutMsg>>
. So tfb1
接收传入消息并创建传出消息列表。此出消息列表应链接到路由器数据块,该路由器数据块接收OutMsg
作为输入(而不是IReadOnlyCollection<OutMsg>
).
我怎样才能压平IReadOnlyCollection
以便包含的消息可以用作例如的输入形式为的变换块TransformBlock<OutMsg, SomeOtherType>
。是否可以通过LinkTo()
?
Thx
您可以使用TransformManyBlock
代替TransformMany
压平任何IEnumerable<T>
结果,例如:
var flattenBlock = new TransformManyBlock<InMsg,OutMsg>(msg=>{
List<OutMsg> myResultList;
//Calculate some results
return myResultList;
});
这会将各个 OutMsg 实例传递到下一个块。
您可以使用迭代器立即传播各个消息:
var flattenBlock = new TransformManyBlock<InMsg,OutMsg>(msg=>{
//Calculate some results
foreach(var item in someDbResults)
{
yield return item;
}
});
或者你可以直接压平输入:
var flattenBlock = new TransformManyBlock<IEnumerable<OutMsg>,OutMsg>(items=>items);
您可以将此块链接到任何接受 OutMsg 的块:
var block = new ActionBlock<OutMsg>(...);
flattenBlock.LinkTo(block);
You can route通过将谓词传递给消息LinkTo
。例如,如果要将失败消息路由到日志记录块,您可以键入:
flattenBlock.LinkTo(logBlock,msg=>msg.HasError);
flattenBlock.LinkTo(happyBlock);
与任何谓词不匹配的消息将卡在输出缓冲区中并阻止块完成。通过不带谓词的一个链接,您可以确保所有消息都将得到处理
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)