Tpl Dataflow (IDataflowBlock) – Part 5

2011/12/19

Tpl Dataflow (IDataflowBlock ) – Part 5

the previous post discus the concept ITargetBlock and ISourceBlock,
which is the TDF consumer/Producer contract.

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.

IDataflowBlock

the IDataflowBlock define single property and 2 methods:

Code Snippet
  1. public interface IDataflowBlock
  2. {
  3.     Task Completion { get; }
  4.  
  5.     void Complete();
  6.     void Fault(Exception exception);
  7. }

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.

Code Snippet
  1. var ab = new ActionBlock<int>(i =>
  2. {
  3.     Thread.Sleep(1000);
  4.     Console.WriteLine(i);
  5. });
  6.  
  7. ab.Completion.ContinueWith(t =>
  8. {
  9.     if (t.Status == TaskStatus.Faulted)
  10.         Console.WriteLine("Failiur: {0}", t.Exception);
  11.     else
  12.         Console.WriteLine("Complete");
  13. });
  14.  
  15. Console.WriteLine("Proccesing");
  16.  
  17. ab.Post(1);
  18. ab.Post(2);
  19. ab.Post(3);
  20.  
  21. if (Console.ReadKey().KeyChar == 'f')
  22.     (ab as IDataflowBlock).Fault(new Exception("wrong tick"));
  23. else
  24.     ab.Complete();

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.

Summary

every dataflow block implement the IDataflowBlock contract which enable to control and monitor the block life-time.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*