簡體   English   中英

將基於C#BlockingCollection的代碼轉換為TPL數據流

[英]Converting my C# BlockingCollection based code to TPL dataflow

我有一個特定的問題,我相信可以使用TPL數據流解決。 我對此很陌生,所以需要你的幫助來加快我的理解。 我的代碼目前是這樣的:

目前的代碼

其中Process1,Process2,Process3均為Task。 對象通過阻塞集合從一個塊傳遞到另一個塊。 我想這樣做:

需要這樣的

我讀到了TransformBlock,ActionBlock和BatchBlock ..您可以幫助我如何使用這些類來實現上述設計。

您沒有提供有關Process2和Process3塊對輸入數據執行的操作的任何詳細信息,因此我假設每個塊都對原始對象進行了一些獨特的轉換,以便對象的輸出列表包含兩個進程的連接結果。

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 . . .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM