2017-06-07 120 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

现在似乎很明显!谢谢。 –

相关问题