MSDN Library

DataflowBlock.Encapsulate(TInput, TOutput) Method

.NET Framework (current version)

Encapsulates a target and a source into a single propagator.

Namespace:  System.Threading.Tasks.Dataflow
Assembly:  System.Threading.Tasks.Dataflow (in System.Threading.Tasks.Dataflow.dll)

static member Encapsulate : 
        target:ITargetBlock<'TInput> * 
        source:ISourceBlock<'TOutput> -> IPropagatorBlock<'TInput, 'TOutput> 

Type Parameters


Specifies the type of input expected by the target.


Specifies the type of output produced by the source.


Type: System.Threading.Tasks.Dataflow.ITargetBlock(TInput)

The target to encapsulate.

Type: System.Threading.Tasks.Dataflow.ISourceBlock(TOutput)

The source to encapsulate.

Return Value

Type: System.Threading.Tasks.Dataflow.IPropagatorBlock(TInput, TOutput)
The encapsulated target and source.

The Encapsulate(TInput, TOutput) method requires two existing blocks: a target block (an instance of a class that implements ITargetBlock(TInput)) and a source block (an instance of a class that implements ISourceBlock(TOutput)). Encapsulate(TInput, TOutput) creates a new instance of an internal class that connects the ITargetBlock(TInput) interface members to the target parameter and the ISourceBlock(TOutput) interface members to the source parameter. Both ITargetBlock(TInput) and ISourceBlock(TOutput) derive from IDataflowBlock. Block completion is explicitly passed from sources to targets. Therefore, the Complete and Fault methods are connected to the target while the Completion property is connected to the source. You must ensure that when the target half completes, the source half gets completed in the most appropriate manner; for example:

target.Completion.ContinueWith(completion => source.Complete());

Or, if you want to propagate the completion type, you can use this more sophisticated code:

target.Completion.ContinueWith(completion => { if (completion.IsFaulted) 3 


You must also explicitly provide the message propagation from target to source. The benefit of this explicit connection is that it gives you the freedom to perform any unconstrained processing between the two encapsulated blocks. You may do that either by encoding the necessary processing into the blocks' delegates (if the blocks take delegates), or by embedding a sub-network of blocks between them. The easier way is to use a block that takes delegates; for example, use ActionBlock(TInput), TransformBlock(TInput, TOutput), TransformManyBlock(TInput, TOutput) (if applicable), or a custom block.

.NET Framework

Supported in: 4.5

Portable Class Library

Supported in: Portable Class Library
© 2016 Microsoft