2017-06-07 44 views
1

我仍然對TPL DataFlow感到厭倦,所以請耐心等待。ActionBlock是否可以鏈接到另一個ActionBlock,其中包含更多參數?

我的應用程序需要並行執行隊列,同時保持其順序。這導致我到DataFlow庫和我想要做的。我想知道是否有一種方法可以將一個ActionBlock鏈接到另一個ActionBlock,而第二個從第一個ActionBlock的值開始操作。
僞例如:

var block1 = new ActionBlock<ByteBuffer>(buffer => { 
    // code generating a hash of the byte buffer to pass to next block 
    ulong hash = generateHash(buffer); 
    // this is what i would like to pass to the next ActionBlock 
    var tup = Tuple<ByteBuffer, ulong>(buffer, along); 
}, dataFlowOpts); 
var block2 = new ActionBlock<Tuple<ByteBuffer, ulong>(tup => { 
    /* code to act on the buffer and hash */ 
}, dataFlowOpts); 

block1.LinkTo(block2); // Is there something like this that would use the correct params? 

正是我試圖做可能嗎?這甚至有意義嗎?我將它們分成兩個ActionBlocks的原因是我想在另一個代碼路徑中重用block2(以不同的方式散列不同ByteBuffer的內容)。

也許有更好的方法嗎?真的,我只是試圖散列對象,因爲它們以併發方式進入,同時保留FIFO順序,因爲它太慢而無法同步散列這些對象。

+0

你看過使用Rx--微軟的Reactive Framework嗎? (NuGet「System.Reactive」) – Enigmativity

回答

2

只需使用TransformBlock

var block1 = new TransformBlock<ByteBuffer, Tuple<ByteBuffer, ulong>>(buffer => { 
    // code generating a hash of the byte buffer to pass to next block 
    ulong hash = generateHash(buffer); 
    // this is what i would like to pass to the next ActionBlock 
    return Tuple<ByteBuffer, ulong>(buffer, along); 
}, dataFlowOpts); 
var block2 = new ActionBlock<Tuple<ByteBuffer, ulong>(tup => { 
    /* code to act on the buffer and hash */ 
}, dataFlowOpts); 

block1.LinkTo(block2); // Is there something like this that would use the correct params? 

或者,可能是更好的選擇,使用DTO類具有兩個屬性:緩存和哈希,如Tuple沒有太大的可讀性。另外,考慮關於命名元組的新C#特性。

+0

現在似乎很明顯!謝謝。 –

相關問題