TPL Dataflow break LinkTo() during runtime
-
27-06-2021 - |
سؤال
I have a TransformManyBlock<Tin, Tout>
and during run time add consumers (ActionBlocks) through LinkTo(...)
.
Is TransformManyBlock the correct data flow block to consume elements, transform them, and then output (same number elements output as input) to several consumers (each linked-to consumer consumes identical elements like a broadcast)? I deliberately do not chose a BroadCastBlock because it seems to be unable to transform elements similarly to a BufferBlock.
I like to know how I can unlink consumers (ActionBlocks here) during runtime? LinkTo() does not seem to provide such functionality as far as I can see.
المحلول
TransformManyBlock
is for transforming one item on the input into multiple items on the output, it has nothing to do with the number of consumers.If you want to perform the transformation separately for each target, then that's exactly what
cloningFunction
inBroadcastBlock
is for.If you want to perform the transformation once and then send each target the same object, you can combine
TransformBlock
andBroadcastBlock
by linking them together. The item will be transformed by theTransformBlock
, and then sent to all targets by theBroadcastBlock
.The return value of
LinkTo()
is:An
IDisposable
that, upon callingDispose
, will unlink the source from the target.This is exactly what you need. Just store the value returned from
LinkTo()
and thenDispose()
it when necessary.