System.AggregateException 在 System.Threading.Tasks.TaskExceptionHolder.Finalize()

System.AggregateException at System.Threading.Tasks.TaskExceptionHolder.Finalize()

更新:

添加 TaskCreationOptions.LongRunning 解决了问题,但这是一个好方法吗?如果不是,克服此异常的最佳解决方案是什么?

我正在尝试解决一个问题。我已经实施了 Whosebug 中提供的建议,但这些建议并没有帮助解决问题。我通过附加扩展方法使用了其他替代方法,例如 ContinuwWith 选项而不是 Task.WaitAll。这也没有帮助。

我已将 Ex.handle { } 放入异常中的 Catch(aggrgateException ex) 中并尝试抛出 ex,但这无助于捕获实际异常。

我只安装了 .Net 4.0,所以我无法尝试 .Net 4.5 分辨率来解决这个问题

我一直遇到的异常是

"System.AggregateException:" The task's exception was not observed either by waiting on the task or accessing the Exception property "

在此之后,它简单地杀死了工作进程,应用程序崩溃了,我在 EventViewer 中看到了一个条目

这里的任何帮助将不胜感激。

我们有以下代码:

Task<List<MyBusinessObject>>[] tasks = new Task<List<MyBusinessObject>>[MyCollection.Count];

for (int i = 0; i < MyCollection.Count; i++)
{
    MyDTO dto = new MyDTO();

     --Some Property Assignment for the MyDTO object--

    tasks[i] = Task<List<MyBusinessObject>>.Factory.StartNew(MyDelegate, dto)

}

try
{
    Task.WaitAll(tasks);
}
catch (AggregateException e)
{
    AddToLogFile("Exceptions thrown by WaitAll() : ");

    for (int j = 0; j < e.InnerExceptions.Count; j++)
    {
        AddToLogFile(e.InnerExceptions[j].ToString());

    }
}  
catch(Exception ex)

{  

AddToLogFile(ex.Message);

}

第二选择

public Static Class Extensions
{
 public static void LogExceptions(this Task<List<<MyBusinessObject>> task)
        {
            task.ContinueWith(t =>
            {
                var aggException = t.Exception.Flatten();
                foreach (var exception in aggException.InnerExceptions)
                {
                    AddToLogFile("Task Exception: " + exception.Message);

                }
            },
            TaskContinuationOptions.OnlyOnFaulted);
        }
}
//In a different class call the extension method after starting the new tasks
Task<List<MyBusinessObject>>[] tasks = new Task<List<MyBusinessObject>>[MyCollection.Count];

for (int i = 0; i < MyCollection.Count; i++)
{
    MyDTO dto = new MyDTO();

     --Some Property Assignment for the MyDTO object--

    tasks[i] = Task<List<MyBusinessObject>>.Factory.StartNew(MyDelegate, dto).LogExceptions()

}

我没有一次为每个迭代创建一个新任务,而是每次为 100 个任务创建一个新任务。然后我等到所有任务都完成了,然后又开始了 100 个任务,直到所有任务都完成

int 计数 = Mycollection.Count(); 任务>[]个任务;

        int i = 0;
                int j;
                while (i < count)
                {
                    j = 0;
                    tasks = new Task<List<MyBusinessObject>>[nbrOfTasks];
                    foreach (var p in Mycollection.Skip(i).Take(nbrOfTasks))
                    {
                        MyRequestDto dto = new MyRequestDto ();
                    --Some Proerty Assignment
                        tasks[j] = Task<List<MyBusinessObject>>.Factory.StartNew(MyDelegate, dto);
                        i++;
                        j++;
                    }

        try
                    {
                        // Wait for all the tasks to finish. 
                        if (tasks != null && tasks.Count() > 0)
                        {
                            tasks = tasks.Where(t => t != null).ToArray();
                            Task.WaitAll(tasks);
                        }
                    }
        catch (AggregateException e)
                    {
            }       

这与可用于处理多个任务的内存有关。如果所需的 RAM 不可用于沿途为并发任务提供服务,则 App Would just crash.That 就是为什么最好生成有限数量的任务。而不是生成多个并将其留给线程池来管理。