using System.Diagnostics;
using System.Threading.Tasks;
public static void Main()
const int timeoutMs = 10;
Action work = () => Thread.Sleep(sleepMs);
long before = Stopwatch.GetTimestamp();
Task showWhatHappensToWork = Task.Run(work).ContinueWith(t =>
long elapsedUntilWorkCompleted = (Stopwatch.GetTimestamp() - before) / TimeSpan.TicksPerMillisecond;
Console.WriteLine("After " + elapsedUntilWorkCompleted.ToString("D3") + " ms: Work " + (t.IsFaulted ? "faulted" : (t.IsCanceled ? "canceled" : "completed (was not canceled)")));
showWhatHappensToWork.Wait(timeoutMs);
long elapsedUntilStoppedWaiting = (Stopwatch.GetTimestamp() - before) / TimeSpan.TicksPerMillisecond;
Console.WriteLine("After " + elapsedUntilStoppedWaiting.ToString("D3") + " ms: Stopped waiting (walked away from waiting)." );
showWhatHappensToWork.Wait();