日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

c#?Task.Wait()與awaiat?Task異常處理的區(qū)別說明_C#教程

作者:皮皮君 ? 更新時(shí)間: 2022-08-02 編程語言

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é)束了,于是就寫了一段測(cè)試代碼進(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());
            //啟動(dòng)任務(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é):(不推薦,請(qǐng)看補(bǔ)充內(nèi)容)

new Task().Start()?中一旦使用 await ,會(huì)立馬返回結(jié)束狀態(tài)。

所以,在使用 Task.WaitAll()? 或其接續(xù)任務(wù)的時(shí)候,可以考慮使用 Thead.sleep() 替代 await? Task.Delay() 。

2022-04-25 補(bǔ)充:

經(jīng)過【32號(hào)就放假】提醒,測(cè)試了Task.Run() 和 Task.Factory.StartNew()兩個(gè)方法,得出結(jié)論:

1、 在Task.Run()啟動(dòng)任務(wù)中,await會(huì)正常運(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() 啟動(dòng)任務(wù)中,會(huì)立馬返回結(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();
        }

原文鏈接:https://blog.csdn.net/qq_16587307/article/details/103954279

欄目分類
最近更新