public static void DelayWithCancellationTest() { CancellationTokenSource cts = new CancellationTokenSource(); Console.WriteLine("Start delay\n"); Task delayTask = AsyncOpers.DelayAsync(5000, cts.Token); delayTask.ContinueWith(ant => { Console.WriteLine("Delay terminated!\n"); }, TaskContinuationOptions.OnlyOnRanToCompletion); Thread.Sleep(2000); // Ugh!! Just for test purpose cts.Cancel(); try { delayTask.Wait(); Console.WriteLine("Ok!\n"); } catch (AggregateException ae) { Console.WriteLine("Error!\n"); ae.Flatten().Handle(e => { Console.WriteLine(e); return(true); }); } }
public static void DelayTest() { Console.WriteLine("Start delay\n"); AsyncOpers.DelayAsync(3000) .ContinueWith(__ => { Console.WriteLine("Delay terminated!\n"); }); }