简体   繁体   English

处理任务并行库中的异常

[英]Handling Exceptions in Task Parallel library

I got the below code from this article on COLIN Mackay's Blog. 我从COLIN Mackay博客上的这篇文章中得到了以下代码。 Tasks that throw exceptions as this article suggest is that exceptions thrown in the task are not bubbled up Unless one of the of the Wait… methods (excluding WaitAny) is called. 本文提出的抛出异常的任务是, 任务中抛出的异常不会被冒泡,除非调用Wait ...方法之一(不包括WaitAny)。 well I have 2 similar scenarios that give two different results. 我有2个类似的场景,给出两个不同的结果。

First Scenario 第一种情景

  1. comment out the part under ( comment this first ) 评论下的部分( 首先评论
  2. leave the part under ( comment this second ) uncommented 留下部分( 评论这一点 )没有评论
  3. run the console application using ctrl+f5 so that the deubugger will not break on the exception. 使用ctrl + f5运行控制台应用程序,以便deubugger不会在异常中中断。
    1. observe that even though the task throw an exception the exception is not bubbled unless the wait method is called.(Press enter twice to call the wait method) 观察即使任务抛出异常,除非调用wait方法,否则异常不会冒泡。(按两次enter键调用wait方法)

第一种情景

Second Scenario 第二种情景

  1. comment out the part under (comment this second) 评论下的部分(评论第二)
  2. leave the part under (comment this first) uncommented 留下部分(首先评论)未注释
  3. run the console application using ctrl+f5 so that the deubugger will not break on the exception. 使用ctrl + f5运行控制台应用程序,以便deubugger不会在异常中中断。
    1. now exception is also not bubbled but the diffrence is that the tasks didnt start as the console doesn't display the status of the task as the first scenario even though both are thrown exceptions. 现在异常也没有冒泡,但差异在于任务没有启动,因为控制台没有将任务的状态显示为第一个场景,即使两者都被抛出异常。 can someone explain this behavior and the diffrence between both exceptions. 有人可以解释这种行为以及两种异常之间的差异。

第二种情况 here is the code. 这是代码。

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Threading
{
    class Program
    {
        static void Main(string[] args)
        {
            // Start the tasks
            List<Task> tasks = new List<Task>();
            for (int i = 0; i < 20; i++)
            {
                Task t = Task.Factory.StartNew(PerformTask);
                tasks.Add(t);
            }

            Console.WriteLine("Press enter to display the task status.");
            Console.ReadLine();

            // Display the status of each task.
            // If it has thrown an exception will be "faulted"
            foreach (Task t in tasks)
                Console.WriteLine("Task {0} status: {1}", t.Id, t.Status);

            Console.WriteLine("Press enter to wait for all tasks.");
            Console.ReadLine();

            // This is where the AggregateException is finally thrown
            Task.WaitAll(tasks.ToArray());

            Console.ReadLine();
        }

        public static void PerformTask()
        {
            //comment this first
            //string input = null;
            //string output = input.ToUpper();
            Console.WriteLine("Starting Task {0}", Task.CurrentId);
            //comment this second
            throw new Exception("Throwing exception in task " + Task.CurrentId);
        }
    }
}

the diffrence is that the tasks didnt start as the console doesn't display the status of the task as the first scenario 差异在于任务没有启动,因为控制台没有将任务的状态显示为第一个场景

That's because the second time, the exception is thrown before your Console.WriteLine method call: 那是因为第二次,在您的Console.WriteLine方法调用之前抛出异常:

string input = null;
string output = input.ToUpper(); // this will throw
Console.WriteLine("Starting Task {0}", Task.CurrentId);

Versus: 与:

Console.WriteLine("Starting Task {0}", Task.CurrentId); // This will first be written
throw new Exception("Throwing exception in task " + Task.CurrentId);

If you want to experience the same behavior, write out to the console at the beginning of your method: 如果您想体验相同的行为,请在方法开头写到控制台:

public static void PerformTask()
{
    Console.WriteLine("Starting Task {0}", Task.CurrentId);

    //string input = null;
    //string output = input.ToUpper();

    throw new Exception("Throwing exception in task " + Task.CurrentId);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM