Export (0) Print
Expand All

DataflowBlock.Encapsulate(TInput, TOutput) Method

.NET Framework 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)

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

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

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft