在哪里处理Task抛出的异常

本文关键字:异常 处理 Task 在哪里 | 更新日期: 2023-09-27 18:03:30

我在单独的任务中执行一些轮询IO循环。这些循环可能会遇到异常。如果遇到异常,我想提醒调用者,以便它可以:

  1. 记录
  2. 杀死所有IO线程
  3. 重置连接
  4. 重启IO线程

UI必须保持响应。处理这种情况的首选方法是什么?我在下面附上了一个说明性的程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TaskExceptionCatching
{
    class Program
    {
        static void Main(string[] args)
        {
            startLoops();
            System.Console.WriteLine("Type 'Exit' when you're ready to stop.");
            while (System.Console.ReadLine() != "Exit")
            {
                System.Console.WriteLine("Seriously, just type 'Exit' when you're ready to stop.");
            }
        }
        static private void startLoops()
        {
            System.Console.WriteLine("Starting fizzLoop.");
            var fizzTask = Task.Factory.StartNew(new Action(fizzLoop));
            System.Console.WriteLine("Starting buzzLoop.");
            var buzzTask = Task.Factory.StartNew(new Action(buzzLoop));
        }
        static private void fizzLoop()
        {
            while (true)
            {
                //simulate something error prone, like some risky IO
                System.Threading.Thread.Sleep(200);
                bool isErr = (new Random().Next(1, 100) == 10);
                if (isErr)
                    throw new Exception("Fizz got an exception.");
            }
        }
        static private void buzzLoop()
        {
            while (true)
            {
                //simulate something error prone, like some risky IO
                System.Threading.Thread.Sleep(200);
                bool isErr = (new Random().Next(1, 100) == 10);
                if (isErr)
                    throw new Exception("Buzz got an exception.");
            }
        }
    }
}

在哪里处理Task抛出的异常

这可能是async void方法可能很方便的罕见情况之一:

static async void StartAndMonitorAsync(Func<Task> taskFunc)
{
    while (true)
    {
        var task = taskFunc();
        try
        {
            await task;
            // process the result if needed
            return;
        }
        catch (Exception ex)
        {
            // log the error
            System.Console.WriteLine("Error: {0}, restarting...", ex.Message);
        }
        // do other stuff before restarting (if any)
    }
}
static private void startLoops()
{
    System.Console.WriteLine("Starting fizzLoop.");
    StartAndMonitorAsync(() => Task.Factory.StartNew(new Action(fizzLoop)));
    System.Console.WriteLine("Starting buzzLoop.");
    StartAndMonitorAsync(() => Task.Factory.StartNew(new Action(buzzLoop)));
}

如果不能使用async/await,可以使用Task.ContinueWith实现类似的逻辑。

如果startLoops可以被多次调用(而任务已经"在飞行"),你需要添加取消逻辑到StartAndMonitorAsync和它启动的任务,使用CancelltionToken(更多的细节在"自我取消和重新启动任务的模式")。

From MSDNTask基础结构将它们封装在AggregateException实例中。AggregateException有一个InnerExceptions属性,可以枚举该属性来检查抛出的所有原始异常,并单独处理(或不处理)每个异常。即使只抛出一个异常,它仍然被包装在AggregateException中。更多信息请查看

try
{
System.Console.WriteLine("Starting fizzLoop.");
            var fizzTask = Task.Factory.StartNew(new Action(fizzLoop));
            System.Console.WriteLine("Starting buzzLoop.");
            var buzzTask = Task.Factory.StartNew(new Action(buzzLoop));
}
catch (AggregateException ae)
{
    // Assume we know what's going on with this particular exception. 
    // Rethrow anything else. AggregateException.Handle provides 
    // another way to express this. See later example. 
    foreach (var e in ae.InnerExceptions)
    {
        if (e is MyCustomException)
        {
            Console.WriteLine(e.Message);
        }
        else
        {
            throw;
        }
    }
}