Search code examples
.netdelegatestask-parallel-libraryblockdataflow

Async/Sync worker delegate in TPL Dataflow block. Which better?


Are there any differences between:

Dim Block As New TransformBlock(Of Integer, Integer)(Function(_i)
                                                     //sync proccessing
                                                       Return j
                                                     End Function)

And

   Dim Block As New TransformBlock(Of Integer, Integer)(Async Function(_i)
                                                       Return Await something
                                                     End Function)

Even if I use a Sync delegate, setting MaxDegreeOfParallelerism of block affects its perfomance. So, as I get it, block makes a Task(Of T) from its sync delegate and works with its instances.

Which of this variants preferably to use?


Solution

  • Use the asynchronous delegate when you have naturally-asynchronous work to do (e.g., I/O-bound).

    Use the synchronous delegate when you have naturally-synchronous work to do (e.g., CPU-bound).

    Neither one is "better;" it just depends on what kind of work you need to do.