9

I am building a Dataflows pipeline whose job it is to process large files. Each file is parsed, analyzed, and rendered; but every file may take a different path through the pipeline, depending on what type of file it is.

The user interface for this pipeline consists of a list of files to be processed, along with a progress bar and a "Cancel" button next to each file (and, of course, a button to add a new file to the queue). When the user clicks the "Cancel" button next to a specific file, I'd like to remove just that one file from the pipeline.

I must be missing something though, because I can't figure out how to do that. I know I can cancel an entire block, but I don't want to do that, I just want to cancel a single item in the pipeline. So, what am I missing ?

Bugmaster
  • 1,048
  • 9
  • 18
  • 1
    How you implemented the pipeline? some code could help. – Orion_Eagle Jul 10 '15 at 19:01
  • My bad, as mentioned in your tags, you might be using System.Threading.Tasks.Dataflow – Orion_Eagle Jul 10 '15 at 19:07
  • Please [check](https://msdn.microsoft.com/en-us/library/hh228600(v=vs.110).aspx) this link, if you could pass individual CancellationTokenSource to each TransformBlock that might help in cancelling them individually. – Orion_Eagle Jul 10 '15 at 19:16

1 Answers1

10

TPL Dataflow doesn't support cancelling specific items out of the box.

You can implement that yourself by creating a wrapper over the item with a matching CancellationToken and posting it to the pipeline instead of just the file. Then just add the code in each block that disregards that file if the token was cancelled and the item will quickly pass through:

var block = new ActionBlock<FileWrapper>(wrapper => 
{
    if (wrapper.CancellationToken.IsCancelltionRequested)
    {
        return;
    }

    ProcessFile(wrapper.File);
});

This means that you have one token per item which allows you to target individual items.

i3arnon
  • 113,022
  • 33
  • 324
  • 344