1. 普通任务
普通任务
2. WaitTask
1 static void Main(string[] args) 2 { 3 Console.WriteLine("Main Thread Processing"); 4 waitTaskFinished(); 5 } 6 7 private static void waitTaskFinished() 8 { 9 var task = new Task<int>(CalculateSum); 10 task.Start(); 11 Console.WriteLine("Main Thread Processing Other"); 12 13 // The main thread would wait the task finished and then continue the following works. 14 task.Wait(); 15 Console.WriteLine(task.Result); 16 Console.WriteLine("Main Thread Processing Finished"); 17 Console.ReadLine(); 18 19 } 20 21 private static int CalculateSum() 22 { 23 Console.WriteLine("Task Proceessing"); 24 int sum = 0; 25 for (int t = 0; t < 100; t++) 26 { 27 sum += t; 28 } 29 Console.WriteLine("Task Proceessing Finished"); 30 return sum; 31 }
等待Task结束
后续可以参考: http://www.cnblogs.com/pengstone/archive/2012/12/23/2830238.html
时间: 2024-10-15 20:39:49