-1

I am new to Dataflow, and I follow this walkthrough How to: Cancel a Dataflow Block.
I click add button first, and then click cancel, but I got exception about "A task was canceled Exception" after clicking cancel button. I fail to find any way to resolve this error.
Any help would be appreciated.
Update: Code for demo:

    public partial class Form1 : Form
{
    CancellationTokenSource cancellationTokenSource;
    TransformBlock<WorkItem, WorkItem> startWork;
    ActionBlock<WorkItem> completeWork;
    ActionBlock<ToolStripProgressBar> incProgress;
    ActionBlock<ToolStripProgressBar> decProgress;
    TaskScheduler uiTaskScheduler;
    public Form1()
    {
        InitializeComponent();
        uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
        Cancel.Enabled = false;
    }

    private void Add_Click(object sender, EventArgs e)
    {
        if (!Cancel.Enabled)
        {
            CreatePipeline();
            Cancel.Enabled = true;
        }

        for (int i = 0; i < 20; i++)
        {
            toolStripProgressBar1.Value++;
            startWork.Post(new WorkItem());
        }
    }

    private async void Cancel_Click(object sender, EventArgs e)
    {
        Add.Enabled = false;
        Cancel.Enabled = false;

        cancellationTokenSource.Cancel();

        try
        {
             await Task.WhenAll(
                completeWork.Completion,
                incProgress.Completion,
                decProgress.Completion);
        }
        catch (OperationCanceledException)
        {

            throw;
        }
        toolStripProgressBar4.Value += toolStripProgressBar1.Value;
        toolStripProgressBar4.Value += toolStripProgressBar2.Value;

        // Reset the progress bars that track the number of active work items.
        toolStripProgressBar1.Value = 0;
        toolStripProgressBar2.Value = 0;

        // Enable the Add Work Items button.      
        Add.Enabled = true;
    }
    private void CreatePipeline()
    {
        cancellationTokenSource = new CancellationTokenSource();

        startWork = new TransformBlock<WorkItem, WorkItem>(workItem =>
        {
            workItem.DoWork(250, cancellationTokenSource.Token);
            decProgress.Post(toolStripProgressBar1);
            incProgress.Post(toolStripProgressBar2);
            return workItem;
        },
        new ExecutionDataflowBlockOptions
        {
            CancellationToken = cancellationTokenSource.Token
        });

        completeWork = new ActionBlock<WorkItem>(workItem =>
        {
            workItem.DoWork(1000, cancellationTokenSource.Token);
            decProgress.Post(toolStripProgressBar2);
            incProgress.Post(toolStripProgressBar3);
        },
        new ExecutionDataflowBlockOptions
        {
            CancellationToken = cancellationTokenSource.Token,
            MaxDegreeOfParallelism = 2
        });

        startWork.LinkTo(completeWork);

        startWork.Completion.ContinueWith(delegate { completeWork.Complete(); },cancellationTokenSource.Token);
        incProgress = new ActionBlock<ToolStripProgressBar>(progress =>
        {
            progress.Value++;
        },
        new ExecutionDataflowBlockOptions
        {
            CancellationToken = cancellationTokenSource.Token,
            TaskScheduler = uiTaskScheduler
        });

        decProgress = new ActionBlock<ToolStripProgressBar>(progress => progress.Value--,
            new ExecutionDataflowBlockOptions
            {
                CancellationToken = cancellationTokenSource.Token,
                TaskScheduler = uiTaskScheduler
            });

    }

    class WorkItem
    {
        public void DoWork(int milliseconds, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested == false)
            {
                Thread.Sleep(milliseconds);
            }
        }
    }
}
Edward
  • 22,080
  • 7
  • 44
  • 80
  • Better post relevant code in question itself instead of posting a link to a huge tutorial. – Evk Nov 29 '17 at 09:46
  • Well if you cancel a task then you should not be surprised to get a TaskCanceledException. It just reflects what you have done – Sir Rufo Nov 29 '17 at 09:48
  • @SirRufo How to avoid this exception instead of exception handling this exception? – Edward Nov 29 '17 at 10:04
  • 2
    Well, do not rethrow the catched exception? - BTW you have an exception handling for that ;o) – Sir Rufo Nov 29 '17 at 10:05
  • Unrelated to your issue but the `startWork.Completion.ContinueWith` is unnecessary. You just need to [Propagate Completion](https://stackoverflow.com/questions/33518074/how-do-i-signal-completion-of-my-dataflow). Also the `incProgress` and `decProgress` `ActionBlock`s aren't doing you much good, they could simply be [Progress](https://stackoverflow.com/questions/28430872/task-run-and-ui-progress-updates) – JSteward Nov 29 '17 at 15:04
  • @JSteward Thanks, I am new to Dataflow, could you share us code related with Propagate and Progree? – Edward Nov 30 '17 at 02:10
  • any reason for vote down? – Edward Nov 30 '17 at 09:03

2 Answers2

2

As @SirRufo pointed out, the solution to your question is simply don't re-throw the exception after you've caught it. But to highlight some of the other techniques you can use with dataflow as discussed in the comments I put together a small sample. I've tried to keep the spirit and intent of your original code intact. To that end; the original code didn't show how the flow would complete normally, as opposed to cancelled, so I left it out here as well.

using System;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using System.Windows.Forms;

namespace WindowsFormsApp1 {
    public partial class Form1 : Form {

        private CancellationTokenSource cancellationTokenSource;
        private TransformBlock<WorkItem, WorkItem> startWork;
        private ActionBlock<WorkItem> completeWork;
        private IProgress<int> progressBar1Value;
        private IProgress<int> progressBar2Value;

        public Form1() {
            InitializeComponent();
            btnCancel.Enabled = false;
        }

        private async void btnAdd_Click(object sender, EventArgs e) {
            if(!btnCancel.Enabled) {
                CreatePipeline();
                btnCancel.Enabled = true;
            }
            var data = Enumerable.Range(0, 20).Select(_ => new WorkItem());
            foreach(var workItem in data) {
                await startWork.SendAsync(workItem);
                progressBar1.Value++;                
            }
        }

        private async void btnCancel_Click(object sender, EventArgs e) {
            btnAdd.Enabled = false;
            btnCancel.Enabled = false;

            cancellationTokenSource.Cancel();

            await completeWork.Completion.ContinueWith(tsk => this.Invoke(new Action(() => this.Text = "Flow Cancelled")), 
                                                       TaskContinuationOptions.OnlyOnCanceled);

            progressBar4.Value += progressBar1.Value;
            progressBar4.Value += progressBar2.Value;

            // Reset the progress bars that track the number of active work items.
            progressBar1.Value = 0;
            progressBar2.Value = 0;

            // Enable the Add Work Items button.      
            btnAdd.Enabled = true;
        }

        private void CreatePipeline() {
            cancellationTokenSource = new CancellationTokenSource();
            progressBar1Value = new Progress<int>(_ => progressBar1.Value++);
            progressBar2Value = new Progress<int>(_ => progressBar2.Value++);

            startWork = new TransformBlock<WorkItem, WorkItem>(async workItem => {
                await workItem.DoWork(250, cancellationTokenSource.Token);
                progressBar1Value.Report(0); //Value is ignored since the progressbar value is simply incremented
                progressBar2Value.Report(0); //Value is ignored since the progressbar value is simply incremented
                return workItem;
            },
            new ExecutionDataflowBlockOptions {
                CancellationToken = cancellationTokenSource.Token
            });

            completeWork = new ActionBlock<WorkItem>(async workItem => {
                await workItem.DoWork(1000, cancellationTokenSource.Token);
                progressBar1Value.Report(0); //Value is ignored since the progressbar value is simply incremented
                progressBar2Value.Report(0); //Value is ignored since the progressbar value is simply incremented
            },
            new ExecutionDataflowBlockOptions {
                CancellationToken = cancellationTokenSource.Token,
                MaxDegreeOfParallelism = 2
            });

            startWork.LinkTo(completeWork, new DataflowLinkOptions() { PropagateCompletion = true });
        }
    }

    public class WorkItem {
        public async Task DoWork(int milliseconds, CancellationToken cancellationToken) {
            if(cancellationToken.IsCancellationRequested == false) {
                await Task.Delay(milliseconds);
            }
        }
    }
}
JSteward
  • 6,105
  • 2
  • 14
  • 25
0

After checking the code, I released that the tasks will be cancelled if I click Cancel.

await Task.WhenAll(
                completeWork.Completion,
                incProgress.Completion,
                decProgress.Completion);   

But, above code Task.WhenAll need all of the tasks return complete status, then the "A task was canceled Exception" throw as expected if it returned cancelled instead of completed.
For a possible way to resolve this issue, we should return Task completed if we cancelled the task, and the code below works for me.

       await Task.WhenAll(
           completeWork.Completion.ContinueWith(task => cancelWork(task, "completeWork"), TaskContinuationOptions.OnlyOnCanceled),
           incProgress.Completion.ContinueWith(task => cancelWork(task, "incProgress"), TaskContinuationOptions.OnlyOnCanceled),
           decProgress.Completion.ContinueWith(task => cancelWork(task, "decProgress"), TaskContinuationOptions.OnlyOnCanceled));

Is it reasonable?

Edward
  • 22,080
  • 7
  • 44
  • 80