c#?Task.Wait()與awaiat?Task異常處理的區(qū)別說明
Task.Wait()與awaiat Task異常處理區(qū)別
Task異常處理
下面有兩個(gè)例子代碼,可以直接復(fù)制粘貼到.net core中運(yùn)行。兩個(gè)代碼要實(shí)現(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之后的代碼其實(shí)都是在新的線程(4線程)中執(zhí)行,而Task.Wait()方法后的線程則是在主線程中執(zhí)行。
因此,await之后的代碼完全以傳統(tǒng)方式處理異常;而Task.Wait()拋出的異常則由于是從新線程往外部線程拋出,所以它是被重新封裝為AggregateException異常拋出。
Task.WaitAll()注意事項(xiàng)
使用Task.WaitAll() 等待多任務(wù)執(zhí)行完畢的時(shí)候發(fā)現(xiàn),等待的任務(wù)還沒結(jié)束,Task.WaitAll() 就先結(jié)束了,于是就寫了一段測試代碼進(jìn)行驗(yàn)證。
先上代碼
static void Main(string[] args) { //建立兩個(gè)任務(wù) Task t1 = new Task(async () => await T1()); Task t2 = new Task(async () => await T2()); //啟動任務(wù) t1.Start(); t2.Start(); //等待任務(wù)完成 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}"); }
再上結(jié)果,注意看T1、T2 Done 和 WaitAll Done的打印時(shí)間:
果然,坑!
Task.WaitAll() 盡然比等待的Task先結(jié)束。
總結(jié):(不推薦,請看補(bǔ)充內(nèi)容)
new Task().Start() 中一旦使用 await ,會立馬返回結(jié)束狀態(tài)。
所以,在使用 Task.WaitAll() 或其接續(xù)任務(wù)的時(shí)候,可以考慮使用 Thead.sleep() 替代 await Task.Delay() 。
2022-04-25 補(bǔ)充:
經(jīng)過【32號就放假】提醒,測試了Task.Run() 和 Task.Factory.StartNew()兩個(gè)方法,得出結(jié)論:
1、 在Task.Run()啟動任務(wù)中,await會正常運(yùn)行;(推薦使用)
static void Main(string[] args) { //建立兩個(gè)任務(wù) Task t1 = Task.Run(T1); Task t2 = Task.Run(T2); //等待任務(wù)完成 Task.WaitAll(t1, t2); Print("WaitAll Done"); Console.ReadLine(); }
2、在Task.Factory.StartNew() 啟動任務(wù)中,會立馬返回結(jié)束狀態(tài)。
static void Main(string[] args) { //建立兩個(gè)任務(wù) Task t1 = Task.Factory.StartNew(T1); Task t2 = Task.Factory.StartNew(T2); //等待任務(wù)完成 Task.WaitAll(t1, t2); Print("WaitAll Done"); Console.ReadLine(); }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
.NET(C#):Emit創(chuàng)建異常處理的方法
.NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下2013-04-04C#調(diào)用WinAPI部分命令的方法實(shí)現(xiàn)
本文主要介紹了C#調(diào)用WinAPI部分命令的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01Unity實(shí)現(xiàn)鼠標(biāo)雙擊與長按的檢測
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)鼠標(biāo)雙擊與長按的檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02c#標(biāo)準(zhǔn)idispose模式使用示例
下面將把C#里實(shí)現(xiàn)IDispose模式的代碼展現(xiàn)出來,大家一起來學(xué)習(xí)一下,它的使用場合也很多的,當(dāng)我們手動對網(wǎng)站,數(shù)據(jù)庫作封裝時(shí),都會用的到2014-02-02WPF實(shí)現(xiàn)繪制扇形統(tǒng)計(jì)圖的示例代碼
這篇文章主要介紹了如何利用WPF繪制扇形統(tǒng)計(jì)圖,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09c#實(shí)現(xiàn)windows遠(yuǎn)程桌面連接程序代碼
本篇文章主要介紹了c#實(shí)現(xiàn)windows遠(yuǎn)程桌面連接程序代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05