private static IDisposable SetTimeout(TimeSpan delay, Action operation)
{
var cancellableInvoker = new ThreadSafeInvoker();
TaskAsyncHelper.Delay(delay).Then(() => cancellableInvoker.Invoke(operation));
// Disposing this return value will cancel the operation if it has not already been invoked.
return new DisposableAction(() => cancellableInvoker.Invoke());
}