81

What's the best way to sleep a certain amount of time, but be able to be interrupted by a IsCancellationRequested from a CancellationToken?

I'm looking for a solution which works in .NET Framework 4.0+.

I'd like to write:

void MyFunc(CancellationToken ct)
{
    //... 
    // simulate some long lasting operation that should be cancelable 
    Thread.Sleep(TimeSpan.FromMilliseconds(10000), ct); 
}

5 Answers 5

151

I just blogged about it here:

CancellationToken and Thread.Sleep

in Short:

var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5));

In your context:

void MyFunc (CancellationToken ct)
{
   //... 
   // simulate some long lasting operation that should be cancelable 
   var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10));
}
8
  • 5
    Onur then you could do it just like that token.WaitHandle.WaitOne(5000)
    – Jakoss
    Commented Dec 5, 2016 at 9:30
  • 5
    Hower, the documentation suggest to avoid using the WaitHandle, unless necessary: "Accessing this property causes a WaitHandle to be instantiated. It is preferable to only use this property when necessary, and to then dispose the associated CancellationTokenSource instance at the earliest opportunity (disposing the source will dispose of this allocated handle)." msdn.microsoft.com/EN-US/library/dd321769(v=VS.100,d=hv.2).aspx
    – dennis_ler
    Commented Feb 20, 2018 at 15:53
  • 5
    Warning/Note: accessing ct.WaitHandle throws an exception if the underlying CancellationTokenSource is disposed. Easy enough to address with an extension method.. ct.WaitForCancellation(timespan) which can return immediately if access throws an exception. Should be used in conjunction with cancellation token checks as could lead to misleading 'no wait' times. Commented Jun 21, 2019 at 16:58
  • 1
    @sodjsn26fr, have a look at the CancellationTokenSource class. It exposes a Token property. Thats your token. Create a new CancellationTokenSource() in a parent scope. Pass its Token property to wherever you want to use it, like in a loop somewhere. If the tokenSource.Cancel() method is called then your Token will be signalled for cancellation
    – Frode
    Commented May 15, 2020 at 20:29
  • 1
    @sodjsn26fr, another thing. You get your token "for free" if you add it as the last parameter in Controller-methods (WebAPI). This token can be used to cancel server-requests on the server
    – Frode
    Commented May 15, 2020 at 20:32
16

Alternatively, I think this is pretty clear:

Task.Delay(waitTimeInMs, cancellationToken).Wait(cancellationToken);

6
  • 1
    It's not C#4.0 but C#4.5.
    – Onur
    Commented Oct 7, 2016 at 20:39
  • 4
    The answer from @Frode is better. The reason? It's all too easy to omit the "Wait();" at the end, there are no compile errors, and this doesn't introduce any delay at all! Been burnt by this enough that I simply prefer to avoid this syntax altogether.
    – Contango
    Commented May 12, 2017 at 14:18
  • 1
    I think you need to handle exceptions as documented at msdn.microsoft.com/en-us/library/… Commented Dec 6, 2017 at 18:04
  • Example code: try { System.Threading.Tasks.Task.Delay(waitTimeInMs, cancellationToken).Wait(); } catch (OperationCanceledException) { } catch (AggregateException aex) { if (null != aex.InnerExceptions) { if (aex.InnerExceptions.Any(inn => inn.GetType() != typeof(System.Threading.Tasks.TaskCanceledException))) { throw; } } } Commented Dec 6, 2017 at 18:05
  • 3
    Is it truly necessary to pass in the CancellationToken to both the delay and wait method call? Wait wont continue to block if the token given to the delay method has cancelled. Commented Oct 26, 2021 at 11:39
4

To cancel an asynchronous operation after a certain amount of time whilst still being able to cancel the operation manually use something like the following

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
cts.CancelAfter(5000);

This will cause a cancellation after five seconds. To cancel the operation your self all you have to do is pass the token into your async method and use the token.ThrowifCancellationRequested() method, where you have set up an event handler somewhere to fire cts.Cancel().

So a full example is:

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
cts.CancelAfter(5000);

// Set up the event handler on some button.
if (cancelSource != null)
{
    cancelHandler = delegate
    {
        Cancel(cts);
    };
    stopButton.Click -= cancelHandler;
    stopButton.Click += cancelHandler;
}

// Now launch the method.
SomeMethodAsync(token);

Where stopButton is the button you click to cancel the running task

private void Cancel(CancellationTokenSource cts)
{
    cts.Cancel();
}

and the method is defined as

SomeMethodAsync(CancellationToken token)
{
    Task t = Task.Factory.StartNew(() => 
        {
            msTimeout = 5000;
            Pump(token);
        }, token,
           TaskCreationOptions.None,
           TaskScheduler.Default);
}

Now, to enable you to work the thread but also enable user cancellation, you will need to write a 'pumping' method

int msTimeout;
bool timeLimitReached = false;
private void Pump(CancellationToken token)
{
    DateTime now = DateTime.Now;
    System.Timer t = new System.Timer(100);
    t.Elapsed -= t_Elapsed;
    t.Elapsed += t_Elapsed;
    t.Start();
    while(!timeLimitReached)
    {
        Thread.Sleep(250);
        token.ThrowIfCancellationRequested();
    }
}

void t_Elapsed(object sender, ElapsedEventArgs e)
{
    TimeSpan elapsed = DateTime.Now - this.readyUpInitialised;
    if (elapsed > msTimeout)
    {
        timeLimitReached = true;
        t.Stop();
        t.Dispose();
    }
}

Note, SomeAsyncMethod will return right to the caller. To block the caller as well you will have to move the Task up in the call hierarchy.

4
  • 2
    Thanks for your very detailed answer. But I don't think your presented solution is what I was looking for. I want to determine the sleep amount in the function not the place where the function is called! The whole point is to mock some long lasting operation. And how log this takes is not of concern to the calling site.
    – Onur
    Commented Sep 10, 2013 at 9:40
  • See edit. Also, see here for difference between SpinWait and Sleep.
    – MoonKnight
    Commented Sep 10, 2013 at 9:43
  • I see two problems with your solution: 1.) It doesn't block execution but returns immediately. This is something a "t.Wait();" could solve. 2.) It can only be cancelled after waiting the amount of time, which is quite pointless since I'm finished already anyway.
    – Onur
    Commented Sep 10, 2013 at 9:52
  • Your solution might work but it looks quite more complicated than the best solution I found so far: stackoverflow.com/a/18715183/1254743
    – Onur
    Commented Sep 10, 2013 at 10:38
3

The CancellationToken.WaitHandle can throw an exception when accessed after the CancellationTokenSource has been disposed:

ObjectDisposedException: The CancellationTokenSource has been disposed.

In some cases, especially when linked cancellation sources are being manually disposed (as they should be), this can be a nuisance.

This extension method allows 'safe cancellation waiting'; however, it should be used in conjunction with checks to, and proper flagging of, the cancellation token's state and/or usage of the return value. This is because it suppresses exceptions to access of the WaitHandle and may return faster than expected.

internal static class CancellationTokenExtensions
{
    /// <summary>
    /// Wait up to a given duration for a token to be cancelled.
    /// Returns true if the token was cancelled within the duration
    /// or the underlying cancellation token source has been disposed.
    /// </summary>
    public static bool WaitForCancellation(this CancellationToken token, TimeSpan duration)
    {
        WaitHandle handle;
        try
        {
            handle = token.WaitHandle;
        }
        catch
        {
            /// The source of the token was disposed (already cancelled)
            return true;
        }

        if (handle.WaitOne(duration))
        {
            /// A cancellation occured during the wait
            return true;
        }
        else
        {
            /// No cancellation occured during the wait
            return false;
        }
    }
}
1
  • What happens if the token is disposed after we've got handle but before running WaitOne()? Is the handle still valid? Why not wrap a try/catch around the whole content? I would also catch just ObjectDisposedException not to hide other possible issues. Commented Nov 2, 2023 at 0:44
2

The best solution I found so far is:

void MyFunc(CancellationToken ct)
{
  //...
  var timedOut = WaitHandle.WaitAny(new[] { ct.WaitHandle }, TimeSpan.FromMilliseconds(2000)) == WaitHandle.WaitTimeout;
  var cancelled = ! timedOut;
}

UPDATE:

The best solution so far is the accepted answer.

1
  • This is exactly what I was looking for. Well done.
    – rhoonah
    Commented Dec 18, 2023 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.