AsValeO
AsValeO

Reputation: 3039

Same delegates with different params in TPL Dataflow blocks

My TPL Dataflow pipeline uses multiple same blocks, the only difference is each of them uses it's personal proxy to send http requests. So WebProxy here is a parameter. I act like this to create them (conceptual simpled example, WebProxy replaced with blockNum Integer):

Private Sub CreateBlocks()
    Dim blocks As New List(Of TransformBlock(Of Integer, Integer))
    For i = 0 To 100
        Dim blockNum As Integer = i
        Dim block As New TransformBlock(Of Integer, Integer)(Function(_arg)
                                                                 Return _arg + blockNum
                                                             End Function)
        blocks.Add(block)
    Next
End Sub

I use a lambda expressions. It causes some troubles. So the question is: How can I replace it with Delegate function. I mean use AddressOf Smthn. I need to pass blockNum to each block somehow.

UPDATE:

There are two kind of troubles why I don't wanna use a lambda:

First: my lambda-expression is too big, it's very hard to work with it and modify it in editor: whole expression became error if something wrong.

Second: I want to create a base class with Dataflow pipeline, and change only blocks delegates in derived classes.

Upvotes: 1

Views: 84

Answers (1)

Jonathan Allen
Jonathan Allen

Reputation: 70327

Why not move all of the code from the lambda to a real function. Then create a small lambda that captures blockNum and passes it to said function?

Upvotes: 3

Related Questions