How to cancel the continuation of the task?

There is a method in my class that creates an object Task<int>using TaskCompletionSource<int>. Then it performs some asynchronous operations and sets the result. I need to know if I can trust the task method ContinueWithor not:

public Task<int> CalculateAsync()
{
    var source = new TaskCompletionSource();
    DoSomeAsyncStuffAndSetResultOf(source);

    source.Task.ContinueWith(t => Console.WriteLine("Is this reliable?"));
    return source.Task;
}

Can the caller CalculateAsyncdo something with the created task and prevent it from continuing?

+5
source share
1 answer

You can pass CancellationTokenin various overloads ContinueWith... which would be the most idiomatic way to just cancel the sequel, IMO.

I do not think that you can do with the parent task the cancellation of continuations. Anyway, I hope not.

EDIT: , , . , . - , , , .

+4

All Articles