c# Task.Wait()与awaiat Task异常处理区别

33 篇文章 1 订阅

Task异常处理

下面有两个例子代码,可以直接复制粘贴到.net core中运行。两个代码要实现的功能完全一样,但是内核却又很大差异。
先看下面用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线程)中执行,而Task.Wait()方法后的线程则是在主线程中执行。因此,await之后的代码完全以传统方式处理异常;而Task.Wait()抛出的异常则由于是从新线程往外部线程抛出,所以它是被重新封装为AggregateException异常抛出。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值