gpt4 book ai didi

c# - 将基于 C# BlockingCollection 的代码转换为 TPL 数据流

转载 作者:行者123 更新时间:2023-11-30 14:24:22 25 4
gpt4 key购买 nike

我有一个特定问题,我确信可以使用 TPL 数据流解决该问题。我对此很陌生,因此需要您的帮助以加快我的理解。我的代码目前是这样的:

Current code

其中 Process1、Process2、Process3 都是 Task。对象通过阻塞收集从一个 block 传递到另一个 block 。我想这样做:

Need like this

我阅读了有关 TransformBlock、ActionBlock 和 BatchBlock 的内容。你能帮我看看如何使用这些类来实现上述设计吗?

最佳答案

您没有提供有关您的 Process2 和 Process3 block 对输入数据执行的操作的任何详细信息,因此我假设它们中的每一个都对原始对象进行了一些独特的转换,以便对象的输出列表包含连接结果这两个过程。

using System;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace TPLDataFlowExample1
{
class Program
{
static void Main(string[] args)
{
var inputListOfObjects = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Process1 block
var process1 = new TransformBlock<int, int>(i => i * 2);
// Broadcast block which passes objects to Process2 and Process3
var broadCast = new BroadcastBlock<int>(null);
// Process2 block
var process2 = new TransformBlock<int, string>(i => $"Process 2: {i}");
// Process3 block
var process3 = new TransformBlock<int, string>(i => $"Process 3: {i}");
// Just simple action block which will print the result
var print = new ActionBlock<string>(s => Console.WriteLine(s));

// Link the output of Process1 block with the input of Broadcast block. Propagate completion to the next block.
process1.LinkTo(broadCast, new DataflowLinkOptions { PropagateCompletion = true });
// Link the output of Broadcast block with the input of Process2 block. Propagate completion to the next block.
broadCast.LinkTo(process2, new DataflowLinkOptions { PropagateCompletion = true });
// Link the output of Broadcast block with the input of Process3 block. Propagate completion to the next block.
broadCast.LinkTo(process3, new DataflowLinkOptions { PropagateCompletion = true });
// Link the output of Process2 block with the input of Print block.
process2.LinkTo(print);
// Link the output of Process2 block with the input of print block.
process3.LinkTo(print);

// We didn't propagate completion to Print block because it must complete when both Process2 and Process3 blocks are in Completed state.
Task.WhenAll(process2.Completion, process3.Completion).ContinueWith(_ => print.Complete());

// Post data to the Process1 block
foreach (var obj in inputListOfObjects)
{
process1.Post(obj);
}

// Mark the Process1 block as complete
process1.Complete();
// Wait for the last block to process all messages
print.Completion.Wait();
}
}
}


// Output:
//
// Process 2: 2
// Process 3: 2
// Process 3: 4
// Process 3: 6
// Process 3: 8
// Process 3: 10
// Process 3: 12
// Process 3: 14
// Process 3: 16
// Process 3: 18
// Process 3: 20
// Process 2: 4
// Process 2: 6
// Process 2: 8
// Process 2: 10
// Process 2: 12
// Process 2: 14
// Process 2: 16
// Process 2: 18
// Process 2: 20
// Press any key to continue . . .

关于c# - 将基于 C# BlockingCollection 的代码转换为 TPL 数据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41331576/

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