Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a Task using CancellationToken?

So I've this code:

//CancelationToken
CancellationTokenSource src = new CancellationTokenSource();
CancellationToken ct = src.Token;
ct.Register(() => Console.WriteLine("Abbruch des Tasks"));
//Task
Task t = new Task(() =>
{
    System.Threading.Thread.Sleep(1000);
    if (ct.IsCancellationRequested)
    {
        try
        {
            //Throw
            ct.ThrowIfCancellationRequested();                        
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine(
                "ThrowIfCancellationRequested() liefert eben eine Exception");
        }
    }             

}, ct);
//Run Task and Cancel
t.Start();
src.CancelAfter(350);
t.Wait();

// Get Information
Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}",
                    t.IsCanceled, t.IsCompleted, t.IsFaulted);

So in this case I canceled my Task but my output in the end is: "Canceled: False . Finished: True . Error: False"

In my opinion it should be "Canceled:True . Finished:False". Why do I get this result? Because I try to catch the exception?

I've tried it without the try - catch block, but then my program stops because of the OperationCanceledException. Can somebody help me?

like image 283
Netherstorm Avatar asked Feb 24 '16 19:02

Netherstorm


People also ask

How do I cancel a CancellationToken?

When given a CancellationToken , you can create a new instance of the token source, assign it's token to the provided token, and cancel it. All other parties that can read this token will see that it's cancellation has been requested.

What happens when CancellationToken is Cancelled?

The associated CancellationToken will be notified of the cancellation and will transition to a state where IsCancellationRequested returns true. Any callbacks or cancelable operations registered with the CancellationToken will be executed.


1 Answers

You're swallowing the exception, thus the task is flagged as finished as you actually handle the exception and it doesn't propagate outwards.

Instead, don't catch the exception inside the delegate, catch it outside:

void Main()
{
    CancellationTokenSource src = new CancellationTokenSource();
    CancellationToken ct = src.Token;
    ct.Register(() => Console.WriteLine("Abbruch des Tasks"));

    Task t = Task.Run(() =>
    {
        System.Threading.Thread.Sleep(1000);
        ct.ThrowIfCancellationRequested();
    }, ct);

    src.Cancel();
    try
    {
        t.Wait();
    }
    catch (AggregateException e)
    {
        // Don't actually use an empty catch clause, this is
        // for the sake of demonstration.
    }

    Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}",
                       t.IsCanceled, t.IsCompleted, t.IsFaulted);
}
like image 142
Yuval Itzchakov Avatar answered Oct 19 '22 11:10

Yuval Itzchakov