Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataflowBlock::Encapsulate<TInput, TOutput> Method

.NET Framework 4.6 and 4.5

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)

public:
generic<typename TInput, typename TOutput>
static IPropagatorBlock<TInput, TOutput>^ Encapsulate(
	ITargetBlock<TInput>^ target, 
	ISourceBlock<TOutput>^ source
)

Type Parameters

TInput

Specifies the type of input expected by the target.

TOutput

Specifies the type of output produced by the source.

Parameters

target
Type: System.Threading.Tasks.Dataflow::ITargetBlock<TInput>

The target to encapsulate.

source
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 

((IDataflowBlock)batchBlock).Fault(completion.Exception); 
else 
batchBlock.Complete(); 
});

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
Show:
© 2015 Microsoft