TPL数据流 - 为什么我的代码不使用并行处理?

时间:2018-03-07 12:03:18

标签: vb.net tpl-dataflow

好的 - 首先,代码......

Imports System.Threading.Tasks
Imports System.Threading.Tasks.Dataflow

Public Class PaymentPipeLine


#Region "Properties"

    Public Property BufferBlock As BufferBlock(Of CPayment)
    Public Property TransformBlock As TransformBlock(Of CPayment, Task)
    Public Property ActionBlock As ActionBlock(Of Task)

    Public Property IncrementPayment As Action

#End Region

#Region "Constructor"
    Public Sub New(action As Action)
        Me.IncrementPayment = action
        BufferBlock = New BufferBlock(Of CPayment)
        TransformBlock = New TransformBlock(Of CPayment, Task)(Function(x As CPayment)
                                                                   Dim t = Task.Run(Sub()
                                                                                        x.process(Nothing)
                                                                                        Me.IncrementPayment.Invoke()
                                                                                    End Sub)
                                                                   Return t
                                                               End Function)
        ActionBlock = New ActionBlock(Of Task)(Sub(x As Task)
                                                   x.Wait()
                                               End Sub)
        Dim dataflowLinkOptions = New DataflowLinkOptions()
        dataflowLinkOptions.PropagateCompletion = True
        BufferBlock.LinkTo(TransformBlock, dataflowLinkOptions)
        TransformBlock.LinkTo(ActionBlock, dataflowLinkOptions)


    End Sub
#End Region

#Region "Methods"
    Public Sub Process()
        ActionBlock.Completion.Wait()
    End Sub

#End Region

End Class

现在,调用它的代码基本上将值发布到BufferBlock中,当它用完了值时将其标记为完成,然后在管道上调用Process()方法......

我原本期望这会导致支付并行处理,但我发现性能没有任何改善(代码的主要部分是对Web服务进行IO调用以处理付款)。我只能得出结论,付款是逐一通过管道......

我如何得到它: a)在添加付款时发送付款(即不等待其他付款首先通过),或者 b)和a一样,但是可能只有5个可以同时通过?

有什么想法吗?非常感谢你能提出的任何建议......

马丁

1 个答案:

答案 0 :(得分:1)

我发现了问题。

我必须在ExecutionDataflowBlockOptions的构造函数中使用ActionBlock对象,并且ExecutionDataflowBlockOptions我必须设置MaximumDegreeOfParallelization