TaskCanceledException when calling Task.Delay with a CancellationToken in an keyboard event

17.7k views Asked by At

I am trying to delay the processing of a method (SubmitQuery() in the example) called from an keyboard event in WinRT until there has been no further events for a time period (500ms in this case).

I only want SubmitQuery() to run when I think the user has finished typing.

Using the code below, I keep getting a System.Threading.Tasks.TaskCanceledException when Task.Delay(500, cancellationToken.Token); is called. What am I doing wrong here please?

CancellationTokenSource cancellationToken = new CancellationTokenSource();

private async void SearchBox_QueryChanged(SearchBox sender, SearchBoxQueryChangedEventArgs args)
{

        cancellationToken.Cancel();
        cancellationToken = new CancellationTokenSource();

    await Task.Delay(500, cancellationToken.Token);

    if (!cancellationToken.IsCancellationRequested)
    {
        await ViewModel.SubmitQuery();
    }
}
6

There are 6 answers

2
Stephen Cleary On BEST ANSWER

That's to be expected. When you cancel the old Delay, it will raise an exception; that's how cancellation works. You can put a simple try/catch around the Delay to catch the expected exception.

Note that if you want to do time-based logic like this, Rx is a more natural fit than async.

0
Collie On

Curiously, the cancellation exception seems to only be thrown when the cancellation token is on Task.Delay. Put the token on the ContinueWith and no cancel exception is thrown:

Task.Delay(500).ContinueWith(tsk => {
   //code to run after the delay goes here
}, cancellationToken.Token);

You can just chain on yet another .ContinueWith() if you really want to catch any cancellation exception - it'll be passed into there.

11
Theodor Zoulias On

Another easy way to suppress the exception of an awaited task is to pass the task as a single argument to Task.WhenAny:

Creates a task that will complete when any of the supplied tasks have completed.

await Task.WhenAny(Task.Delay(500, token)); // Ignores the exception

One problem of this approach is that it doesn't communicate clearly its intention, so adding a comment is advisable. Another one is that it results in an allocation (because of the params in the signature of Task.WhenAny).


Note: When a Task is faulted and its Exception in not observed directly or indirectly, the TaskScheduler.UnobservedTaskException event is triggered for this task, at some point in the future (non-deterministically). The trick shown above, that uses the Task.WhenAny method, does not observe the Exception of the task, so the event will be triggered. In the specific case of the Task.Delay(500, token), the task completes as canceled, not as faulted, so it doesn't trigger the event anyway.

0
lilo0 On

I think it deserves to add a comment about why it works that way.

The doc is actually wrong or written unclear about the TaskCancelledException for Task.Delay method. The Delay method itself never throws that exception. It transfers the task into cancelled state, and what exactly raises the exception is await. It didn't matter here that Task.Delay method is used. It would work the same way with any other cancelled task, this is how cancellation is expected to work. And this actually explains why adding a continuation mysteriously hides the exception. Because it's caused by await.

0
Michael A. On

If you don't care about any exception that may be thrown by the awaited Task you can create an extension method like this:

public static class TaskExtensions
{
    public static async Task<bool> TryAsync(this Task task)
    {
        try
        {
            await task;
            return true;
        }
        catch
        {
            return false;
        }
    }
}

And then use it like this:

var wasCompleted = await Task.Delay(500, cancellationToken).TryAsync();
if (wasCompleted)
{
    // do something
}

Additional

As a compliment you could add a similar extension method for Task<T> to the TaskExtensions class.

public static async Task<(bool wasCompleted, T result)> TryAsync<T>(this Task<T> task)
{
    try
    {
        var result = await task;
        return (true, result);
    }
    catch
    {
        return (false, default(T));
    }
}

Usage:

var (wasCompleted, result) = await GetSomethingAsync().TryAsync();
if (wasCompleted)
{
    var x = result.X;
}
14
Jeroen van Langen On

If you add ContinueWith() with an empty action, the exception isn't thrown. The exception is caught and passed to the tsk.Exception property in the ContinueWith(). But It saves you from writing a try/catch that uglifies your code.

await Task.Delay(500, cancellationToken.Token).ContinueWith(tsk => { });

UPDATE:

Instead of writing code to handle the exception, a boolean would be much cleaner. This is only preferred when a delay cancel is expected!. One way is to create a helper class (Although I don't like helper classes much)

namespace System.Threading.Tasks
{
    public static class TaskDelay
    {
        public static Task<bool> Wait(TimeSpan timeout, CancellationToken token) =>
            Task.Delay(timeout, token).ContinueWith(tsk => tsk.Exception == default);

        public static Task<bool> Wait(int timeoutMs, CancellationToken token) =>
            Task.Delay(timeoutMs, token).ContinueWith(tsk => tsk.Exception == default);
    }
}

For example:

var source = new CancellationTokenSource();

if(!await TaskDelay.Wait(2000, source.Token))
{
    // The Delay task was canceled.
}

(don't forget to dispose the source)