Cancelling specific items in a dataflow pipeline
Asked Answered
P

1

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 ?

Propagandism answered 10/7, 2015 at 18:59 Comment(3)
How you implemented the pipeline? some code could help.False
My bad, as mentioned in your tags, you might be using System.Threading.Tasks.DataflowFalse
Please check this link, if you could pass individual CancellationTokenSource to each TransformBlock that might help in cancelling them individually.False
Q
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.

Quadrennial answered 10/7, 2015 at 19:16 Comment(2)
thanks, that makes sense. I considered passing my own flag, but your idea is better.Propagandism
I think this is exactly what I need, but I am not sure how to define the FileWrapper object in the example above.Biggerstaff

© 2022 - 2024 — McMap. All rights reserved.