public static async Task<int> CalcAsync(int wait, int shit)
{
await Task.Run(() => Thread.Sleep(wait));
return shit;
}
public static async void TestDoubleTask()
{
Task<int> t1 = CalcAsync(1000, 1); // t1 開始運行
Task<int> t2 = CalcAsync(2000, 2); // t2 開始運行
int r1 = await t1;
int r2 = await t2; // 等待 t1, t2 均完成
Console.WriteLine(r1);
Console.WriteLine(r2);
}
public static void Main()
{
TestDoubleTask();
Console.ReadKey();
}
C# 同時運行多個 Task
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.