Troopers Troopers - 1 month ago 17
C# Question

Tasks cancellation and continue with

I'm perplex on the cancellation of several tasks and continue with a task to display result.
According to what I understood, this program should display

Tasks canceled

CancellationTokenSource cts = new CancellationTokenSource();
List<Task> tasks = new List<Task>();
for(int i= 0; i<3; i++)
tasks.Add(Task.Run(() =>{

// Uncomment this to see 'Tasks canceled' in the result
// cts.Token.ThrowIfCancellationRequested();

Task.WhenAll(tasks).ContinueWith(task =>
Console.WriteLine("Tasks canceled");
Console.WriteLine("Tasks completed");

Unfortunately it display

Tasks completed

If i uncomment the throwing of cancel exception the program display

Tasks canceled

Tasks completed

Why? it seems that I missed something but I do not see what...


In first case this is what your code says it to do - stop looping when cancellation is requesting. After it exits loop, there is nothing more to execute hence task is complete.

By design to really cancel task, you need to throw exception to change execution flow. In fact, 'if' statement is not needed in your code sample - that 'ThrowIfCancellationRequested' method will handle this check by itself and will throw exception if cancellation has been requested.

Regarding last part, here is excerpt from MSDN:

IsCompleted will return true when the task is in one of the three final states: RanToCompletion, Faulted, or Canceled.

So even if task was cancelled, it is complete.