gpt4 book ai didi

C# TPL 数据流 - 完成不起作用

转载 作者:行者123 更新时间:2023-11-30 20:41:56 29 4
gpt4 key购买 nike

此代码永远不会到达最后一行,因为完成不会从 saveBlock 传播到 sendBlock。我做错了什么?

var readGenerateBlock = new TransformBlock<int, int>(n =>
{
Console.WriteLine("Read " + n);
Thread.Sleep(15);
return n;
});
var groupingBlock = new BatchBlock<int>(10);
var saveBlock = new TransformManyBlock<int[], int>(n =>
{
Console.WriteLine("Saving {0} items [{1}; {2}]", n.Count(), n.First(), n.Last());
Thread.Sleep(150);
return n;
});
var sendBlock = new TransformBlock<int, int>(n =>
{
Console.WriteLine("Sending {0}", n);
Thread.Sleep(25);
return n;
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 });

readGenerateBlock.LinkTo(groupingBlock, new DataflowLinkOptions { PropagateCompletion = true });
groupingBlock.LinkTo(saveBlock, new DataflowLinkOptions { PropagateCompletion = true });
saveBlock.LinkTo(sendBlock, new DataflowLinkOptions { PropagateCompletion = true });

Parallel.For(0, 250, i => readGenerateBlock.Post(i));
readGenerateBlock.Complete();

sendBlock.Completion.Wait();
Console.WriteLine("Completed.");

最佳答案

在完成之前,您必须从 block 中读取数据。由于 noöne 正在读取 saveBlock,因此它永远不会完成。

如果您不需要数据,最简单的解决方案是使用 ActionBlock 而不是 TransformBlock。否则,继续读取数据直到 block 完成。

关于C# TPL 数据流 - 完成不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31915295/

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