private static void tt2() { Task task = null; for (int i = 0; i < 10; i++) { task = Task.Factory.StartNew(callback, i); } Thread.Sleep(10000000); } private static void callback(object i) { Console.WriteLine("任務"+i.ToString()+"開始-執行"); Thread.Sleep(10000); Console.WriteLine("任務" + i.ToString() + "結束"); }
或者
private static void tt3() { for (int i = 0; i < 10; i++) { Task task = new Task(callback, i); task.Start(); } Thread.Sleep(10000000); } private static void callback(object i) { Console.WriteLine("任務"+i.ToString()+"開始-執行"); Thread.Sleep(10000); Console.WriteLine("任務" + i.ToString() + "結束"); }
執行截圖