LaVOZs

The World’s Largest Online Community for Developers

'; c# - TaskCanceledException when calling Task.Delay with a CancellationToken in an keyboard event - LavOzs.Com

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();
    }
}

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.

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

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

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.

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 recommended. Another one is that it results in an allocation (because of the params in the signature of Task.WhenAny).

Related
Best practice for consistently cancelling Async CancellationTokenSource
Windows 8 - await Task<Bool> - async call back on completed listener required
Error in Windows 8 xaml Modern app with thread
await async lambda in ActionBlock
When to use Task.Delay, when to use Thread.Sleep?
Using Task.Delay and a recursive call
UWP waiting for MediaPlayer to complete before continuing
Trouble understanding async and await