c#?Task.Wait()與awaiat?Task異常處理的區(qū)別說明
Task.Wait()與awaiat Task異常處理區(qū)別
Task異常處理
下面有兩個例子代碼,可以直接復制粘貼到.net core中運行。兩個代碼要實現(xiàn)的功能完全一樣,但是內(nèi)核卻又很大差異。
先看下面用await的例子與輸出:
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
System.Console.WriteLine($"Main Task ID:{Thread.CurrentThread.ManagedThreadId}");
var task = Task.Run(() =>
{
System.Console.WriteLine($"In Task.Run(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
int[] vary=new int[5];
while (true)
{
Thread.Sleep(3000);
int d = vary[6];
}
});
// Just continue on this thread, or await with try-catch:
try
{
await task;
}
catch (IndexOutOfRangeException ex)
{
System.Console.WriteLine(ex.Message);
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
catch(AggregateException ex)
{
System.Console.WriteLine(ex.Message);
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
finally
{
//...
}
System.Console.WriteLine("Reach end.");
Console.ReadKey();
}
}
/*
Main Task ID:1
In Task.Run(), Task ID:4
Index was outside the bounds of the array.
Catch System.IndexOutOfRangeException
After Wait(), Task ID:4
Reach end.
*/
再看Task.Wait()方法下的異常處理與輸出:
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main()
{
System.Console.WriteLine($"Main Task ID:{Thread.CurrentThread.ManagedThreadId}");
var task = Task.Run(() =>
{
System.Console.WriteLine($"In Task.Run(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
int[] vary=new int[5];
while (true)
{
Thread.Sleep(3000);
int d = vary[6];
}
});
// Just continue on this thread, or await with try-catch:
try
{
task.Wait();
}
catch (IndexOutOfRangeException ex)
{
System.Console.WriteLine($"Catch {ex.GetType()}");
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
catch(AggregateException ex)
{
System.Console.WriteLine($"Catch {ex.GetType()}");
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
finally
{
//...
}
System.Console.WriteLine("Reach end.");
Console.ReadKey();
}
}
/*
Main Task ID:1
In Task.Run(), Task ID:4
Catch System.AggregateException
One or more errors occurred. (Index was outside the bounds of the array.)
After Wait(), Task ID:1
Reach end.
*/
從例子中可以看出,await之后的代碼其實都是在新的線程(4線程)中執(zhí)行,而Task.Wait()方法后的線程則是在主線程中執(zhí)行。
因此,await之后的代碼完全以傳統(tǒng)方式處理異常;而Task.Wait()拋出的異常則由于是從新線程往外部線程拋出,所以它是被重新封裝為AggregateException異常拋出。
Task.WaitAll()注意事項
使用Task.WaitAll() 等待多任務執(zhí)行完畢的時候發(fā)現(xiàn),等待的任務還沒結束,Task.WaitAll() 就先結束了,于是就寫了一段測試代碼進行驗證。
先上代碼
static void Main(string[] args)
{
//建立兩個任務
Task t1 = new Task(async () => await T1());
Task t2 = new Task(async () => await T2());
//啟動任務
t1.Start();
t2.Start();
//等待任務完成
Task.WaitAll(t1, t2);
Print("WaitAll Done");
Console.ReadLine();
}
static async Task T1()
{
Print("T1 Start");
Thread.Sleep(1000);
Print("T1 await");
await Task.Delay(1000);
Print("T1 Done");
}
static async Task T2()
{
Print("T2 Start");
Thread.Sleep(1000);
Print("T2 await");
await Task.Delay(1000);
Print("T2 Done");
}
static void Print(string msg)
{
Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff")}: {msg}");
}再上結果,注意看T1、T2 Done 和 WaitAll Done的打印時間:

果然,坑!
Task.WaitAll() 盡然比等待的Task先結束。
總結:(不推薦,請看補充內(nèi)容)
new Task().Start() 中一旦使用 await ,會立馬返回結束狀態(tài)。
所以,在使用 Task.WaitAll() 或其接續(xù)任務的時候,可以考慮使用 Thead.sleep() 替代 await Task.Delay() 。
2022-04-25 補充:
經(jīng)過【32號就放假】提醒,測試了Task.Run() 和 Task.Factory.StartNew()兩個方法,得出結論:
1、 在Task.Run()啟動任務中,await會正常運行;(推薦使用)
static void Main(string[] args)
{
//建立兩個任務
Task t1 = Task.Run(T1);
Task t2 = Task.Run(T2);
//等待任務完成
Task.WaitAll(t1, t2);
Print("WaitAll Done");
Console.ReadLine();
}
2、在Task.Factory.StartNew() 啟動任務中,會立馬返回結束狀態(tài)。
static void Main(string[] args)
{
//建立兩個任務
Task t1 = Task.Factory.StartNew(T1);
Task t2 = Task.Factory.StartNew(T2);
//等待任務完成
Task.WaitAll(t1, t2);
Print("WaitAll Done");
Console.ReadLine();
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
.NET(C#):Emit創(chuàng)建異常處理的方法
.NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下2013-04-04

