var task = Task.Run(() => { System.Threading.Thread.Sleep(1000 * 40); Console.Write("111"); }); var taskSuccess = task.Wait(1000 * 30); if (!taskSuccess) { Console.WriteLine(task.IsCompleted); Console.Write("222"); }
结果:
False 222
var task = Task.Run(() => { System.Threading.Thread.Sleep(1000 * 20); Console.WriteLine("111"); }); var taskSuccess = task.Wait(1000 * 30); if (!taskSuccess) { Console.WriteLine(task.IsCompleted); Console.Write("222"); } else { Console.WriteLine(task.IsCompleted); Console.WriteLine("333"); }
结果:
111 true 333