public void ContinueWithTest02() { Task <string> task = Task.Run <string>(() => PiCalculator.Calculate(10)); // 本程序中 faultedTask 和 canceledTask 不可能发生,所以不可以等待。 Task faultedTask = task.ContinueWith((antecedentTask) => { Trace.Assert(task.IsFaulted); Console.WriteLine("Task State:Faulted"); }, TaskContinuationOptions.OnlyOnFaulted); Task canceledTask = task.ContinueWith((antecedentTask) => { Trace.Assert(task.IsCanceled); Console.WriteLine("Task State:Canceled"); }, TaskContinuationOptions.OnlyOnCanceled); Task completedTask = task.ContinueWith((antecedentTask) => { Trace.Assert(task.IsCompleted); Console.WriteLine("Task State:Completed"); }, TaskContinuationOptions.OnlyOnRanToCompletion); completedTask.Wait(); }
public void RunTest01() { Task <string> task = Task.Run <string>(() => PiCalculator.Calculate(100)); foreach (var busySymbols in Utility.BusySymbols()) { if (task.IsCompleted) { Console.Write($"IsCompleted:{task.IsCompleted}"); break; } Console.Write(busySymbols); } Console.WriteLine(); Console.WriteLine(task.Result); System.Diagnostics.Trace.Assert(task.IsCompleted); }