New Feature? :: Delayed cancellation

Danny Shih


We’re interested in adding support for scheduling cancellation.  For example:


// Create a token source that will Cancel() after a delay
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));

// And/or schedule a Cancel() call

We’ve heard from many folks that this is a desired feature and have found good uses for it ourselves.  Interestingly, we would also allow CancelAfter() to be called multiple times to reset the delay (provided the token source was not already canceled, of course).  One could also effectively cancel the cancellation by calling CancelAfter() with TimeSpan.MaxValue.


One potential issue we’ve been thinking through is whether/how to deal with exceptions that fly out of user-registered delegates.  Recall that we strongly discourage throwing exceptions from callbacks registered with a CancellationToken, though it is possible to handle such exceptions today:


cts.Token.Register(() => { throw new Exception(“HA!”); });

try { cts.Cancel(); } // invokes the callbacks
catch { // Caught it! }

However, if you use this new feature to cancel asynchronously, you won’t ever have a chance to handle the exception, and it would likely bring down your application.  Some are of the opinion that we need not do anything about this, but we’ve been brainstorming potential solutions anyway.  We’re definitely curious what you think =).  Feel free to answer these questions if you want:

  1. What do you think about unhandled exceptions flying out of user-created token callbacks?  Should we support this use case?
  2. Any feedback about the general feature?




Discussion is closed.

Feedback usabilla icon