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

前端之家收集整理的这篇文章主要介绍了c# – 如何在TPL Dataflow中将多个目标块与源块链接?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我预计以下几个方面可以从两个出版商那里获得产出,但是它只产生第一个输出
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链接到一个TransformBlock.您应该将2个ActionBlock链接到BrodcastBlock并将BroadcastBlock链接到TransformBlock.

你有什么:

BroadCast => Transfrom => ActionBlock
                       => ActionBlock

你需要什么

Transfrom => BroadCast => ActionBlock
                       => ActionBlock
原文链接:https://www.f2er.com/csharp/96347.html

猜你在找的C#相关文章