Tpl Dataflow (IDataflowBlock ) – Part 5
you can find all the post in this series under the TDF tag.
this post focus on the IDataflowBlock contract which is the life-time management contract for all data-flow’s blocks.
the IDataflowBlock define single property and 2 methods:
ending the processing of Dataflow block is done either by calling Complete() or by Fault() in cases that the data flow should exit into fault state.
in case of Complete the block will finish the processing of all messages that already in it’s inner buffer and decline (DecliningPermanently) any incoming messages.
Fault put the block into faulty state and it does not schedule any messages from its inner buffer.
the Completion property is using the TAP (task asynchrony pattern) concept as it API for monitoring the block state.
the completion property return a Task which enable either waiting (Wait) on, continuation (ContinueWith) or await.
the block will set the task completion when it will complete the processing of all the messages within it inner buffer (or when finishing current executing messages in case of faulty state).
because the block is using the task semantic for it’s easy to handle block exceptions.
the following code demonstrate the management of ActionBlock lifetime.
at line 7 we have set a continuation callback which check the completion status and writing the completion information.
point of interest
as we discuss in the previous post the dataflow blocks does have internal task which is responsible for its execution (by default it is one task per block but it can be throttle to work with multiple tasks) this task does release when ever the block become idle (when the block will become active again it will construct new task).
it is important to understand that the completion task is a semantic task and not one of the worker tasks.
in case that you want to create you’re own custom block you can use the TaskCompletionSource<T> in order to present the TAP (task asynchrony pattern) semantics.
you can learn more on the TAP concept in here.
every dataflow block implement the IDataflowBlock contract which enable to control and monitor the block life-time.