gpt4 book ai didi

c# - 如何在 TPL Dataflow 中将多个目标 block 与源 block 链接?

转载 作者:行者123 更新时间:2023-12-03 18:52:30 25 4
gpt4 key购买 nike

我预计以下内容会从两个出版商产生输出,但它只产生第一个的输出:

var broadcastBlock = new BroadcastBlock<int>(null);
var transformBlock = new TransformBlock<int, int>(i => i*10);
var publish1 = new ActionBlock<int>(i => Console.WriteLine("Publisher 1:" + i));
var publish2 = new ActionBlock<int>(i => Console.WriteLine("Publisher 2:" + i));

broadcastBlock.LinkTo(transformBlock, new DataflowLinkOptions() { PropagateCompletion = true });
transformBlock.LinkTo(publish1, new DataflowLinkOptions() { PropagateCompletion = true });
transformBlock.LinkTo(publish2, new DataflowLinkOptions() { PropagateCompletion = true });

foreach (var i in Enumerable.Range(0, 5))
{
broadcastBlock.Post(i);
}
broadcastBlock.Complete();
Task.WhenAll(publish1.Completion, publish2.Completion).Wait();

我显然在这里遗漏了一些基本的东西,有什么想法吗?

最佳答案

您正在链接 2 ActionBlock s 到单个 TransformBlock .您应该链接 2 ActionBlock s 到 BrodcastBlock并链接 BroadcastBlockTransformBlock .

你有什么:

BroadCast => Transfrom => ActionBlock
=> ActionBlock

你需要什么:
Transfrom => BroadCast => ActionBlock
=> ActionBlock

关于c# - 如何在 TPL Dataflow 中将多个目标 block 与源 block 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23290989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com