I am using TPL blocks to carry out operation that may be cancelled by user:
I have come up with two options, in first I cancel whole block but don't cancel operation inside the block, like this:
_downloadCts = new CancellationTokenSource();
var processBlockV1 = new TransformBlock<int, List<int>>(construct =>
{
List<int> properties = GetPropertiesMethod(construct );
var entities = properties
.AsParallel()
.Select(DoSometheningWithData)
.ToList();
return entities;
}, new ExecutionDataflowBlockOptions() { CancellationToken = _downloadCts.Token });
and the second I cancel inside operation, but not the block itself:
var processBlockV2 = new TransformBlock<int, List<int>>(construct =>
{
List<int> properties = GetPropertiesMethod(construct);
var entities = properties
.AsParallel().WithCancellation(_downloadCts.Token)
.Select(DoSometheningWithData)
.ToList();
return entities;
});
As I understand, first option will cancel whole block, thus shutting down the whole pipeline. My question is will it also cancel inside operation and dispose of all resources if there are any(open StreamReaders, ect.) or is it better to choose second option for then I myself can make sure everything is cancelled and cleaned and then I could use some means(railway programming) to float raised OperationCanceledException down the pipe and deal with it where I want?