Ryszard Dżegan
Ryszard Dżegan

Reputation: 25434

Task.IsCancelled doesn't work

I've got the following sample code:

static class Program
{
    static void Main()
    {
        var cts = new CancellationTokenSource();

        var task = Task.Factory.StartNew(
            () =>
                {
                    try
                    {
                        Console.WriteLine("Task: Running");
                        Thread.Sleep(5000);
                        Console.WriteLine("Task: ThrowIfCancellationRequested");
                        cts.Token.ThrowIfCancellationRequested();
                        Thread.Sleep(2000);
                        Console.WriteLine("Task: Completed");
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine("Task: " + exception.GetType().Name);
                        throw;
                    }
                }).ContinueWith(t => Console.WriteLine("ContinueWith: cts.IsCancellationRequested = {0}, task.IsCanceled = {1}, task.Exception = {2}", cts.IsCancellationRequested, t.IsCanceled, t.Exception == null ? "null" : t.Exception.GetType().Name));

        Thread.Sleep(1000);

        Console.WriteLine("Main: Cancel");
        cts.Cancel();

        try
        {
            Console.WriteLine("Main: Wait");
            task.Wait();
        }
        catch (Exception exception)
        {
            Console.WriteLine("Main: Catch " + exception.GetType().Name);
        }

        Console.WriteLine("Main: task.IsCanceled = {0}", task.IsCanceled);
        Console.WriteLine("Press any key to exit...");

        Console.ReadLine();
    }
}

The output is:

If I remove ContinueWith, then the output is:

I don't understand, why task.IsCanceled returns false in both cases?

And why exception is rethrown only without ContinueWith?


What I'm trying to achieve is an uniform and simple way for waiting for task completion and a property that will indicate if task was cancelled or not.

Upvotes: 2

Views: 1694

Answers (1)

alex
alex

Reputation: 12654

I think you are not cancelling the task itself, but just throwing an exception from a task. Try using StartNew(Action action,CancellationToken cancellationToken) instead of StartNew(Action action). You can also add cancellation token as a parameter to ContinueWith.

Upvotes: 6

Related Questions