Redigera

Dela via


DataflowBlock.Encapsulate<TInput,TOutput> Method

Definition

Encapsulates a target and a source into a single propagator.

public:
generic <typename TInput, typename TOutput>
 static System::Threading::Tasks::Dataflow::IPropagatorBlock<TInput, TOutput> ^ Encapsulate(System::Threading::Tasks::Dataflow::ITargetBlock<TInput> ^ target, System::Threading::Tasks::Dataflow::ISourceBlock<TOutput> ^ source);
public static System.Threading.Tasks.Dataflow.IPropagatorBlock<TInput,TOutput> Encapsulate<TInput,TOutput> (System.Threading.Tasks.Dataflow.ITargetBlock<TInput> target, System.Threading.Tasks.Dataflow.ISourceBlock<TOutput> source);
static member Encapsulate : System.Threading.Tasks.Dataflow.ITargetBlock<'Input> * System.Threading.Tasks.Dataflow.ISourceBlock<'Output> -> System.Threading.Tasks.Dataflow.IPropagatorBlock<'Input, 'Output>
Public Function Encapsulate(Of TInput, TOutput) (target As ITargetBlock(Of TInput), source As ISourceBlock(Of TOutput)) As IPropagatorBlock(Of 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
ITargetBlock<TInput>

The target to encapsulate.

source
ISourceBlock<TOutput>

The source to encapsulate.

Returns

The encapsulated target and source.

Remarks

The Encapsulate 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 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)    

((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.

Applies to