TPL – Continuation
this post will discuss TPL Continuation.
TPL continuation can chain task into a pipeline.
when dealing with dependencies between parallel work units, like [encoding -> compression -> encryption], continuation is the API for scheduling work unit upon completion of other work unit.
the general idea is quit similar to the old APM pattern (BeginXxx, EndXxx) callback.
the syntax of continuation:
continuation API is fairly straight-forward:
we can define a continuation action upon task completion (the t in the lambda represent the completed task).
completion of Task<T>
continuation does also support Task<T> this way we can handle a task result upon the task’s completion.
the following sample show the concept of the [encoding -> encryption -> send] pipeline:
the first task (line 1-4) return (async) encoding data.
the first continuation task (line 5-8) get the encoded data from the result of the completed task and encrypt it, the encrypted data return as the task’s result.
the second continuation (line 9-12) will be schedule on the completion of the encryption task.
completion API does not limit to single completion per a task. the completion represent a callback and we can set as many callback as we need for any Task (or Task<T>).
Continue when all/any
we can also set continuation which will be trigger upon the completion of multiple tasks.
or continue on the completion of the first among multiple tasks.
Parent / Child
as you may know TPL support a parent / child execution model,
when you start a task within an executing task scope you can set the task behavior to accept the parent / child paradigm.
the TPL infrastructure does aware when a task is having children and behave accordantly (wait will wait for the completion of all the task’s children, cancelling a parent task will affect all of its children, the debug parallel tasks window can present the task’s hierarchic).
Parent / Child and continuation
when it come to continuation the continuation callback will occurs only after the completion of all the task’s children.
the above code demonstrate a simple continuation upon parent child task.
the completion will occurs when after the completion of t1.
Parent / child with nested continuation
let take another scenario when both the parent and the child task is having a continuation.
let think of the above code. will the parent continuation complete before or after the child continuation?
the answer is: the parent continuation will ignore the child continuation and complete first.
the parent continuation will be aware of the child continuation only if we mark the child continuation with TaskContinuationOptions.AttachedToParent.
now the parent continuation will complete after the completion of the child’s task continuation.
till now we have seen many of the continuation scenarios. the last scenario which I want to present is the cool ability of tuning the continuation to occurs only when the execution status end with specific condition.
you can set the continuation to occur only on success, failure or cancellation.
the TaskContinuationOptions is a bitwise so you can specify multiple option upon single continuation.
continuation is one of the most powerful feature of the new TPL infrastructure.
it is having more feature and it simpler to use than the old APM pattern.
using the continuation pattern we can manage complex parallelism with regard of dependencies.
finally ,as I will describe in latter past, the new async feature (of .NET 4.5 / C#5) is all about the continuation concept.