How to handle partial success in a batch of Tasks?

戏子无情 提交于 2019-12-22 05:28:14

问题


I have 3 Tasks that should run in parallel. I need to await all 3 and then continue processing their results. So something like:

var results = await Task.WhenAll(task1, task2, task3);
return process(results);

This works fine when all 3 tasks complete successfully. However, if one of them fails I get an exception that bubbles up all the way. This is not what I want. If any of these tasks throws an InvalidOperationException I can continue processing, I just need the process method to have access to the exception that was thrown.

I realize that I can try...catch within the tasks but that does not seem like it would be a hack. Beyond being semantically incorrect (the task DID fail - it should not return successfully), if I went with this approach, since the result can be successful OR return an exception, I would have to return a custom type. The Task<T> object however already exposes an Exception channel so I would prefer to not do double-duty and just use what is already present.


回答1:


You can handle this case like in my example below:

Task<T>[] tasks  = new Task<T>[] { task1, task2, task3 };

try
{
   await Task.WhenAll(tasks);
}
catch(InvalidOperationException exception) // If you need to handle any other exception do it
{
   // handle it if you wan to 
}

// You can use task1.IsFauled to know if current task failed, 
// and you can use task1.Exception to know why it failed

return process(tasks.Where(t => !t.IsFauled).ToArray());



回答2:


If I understand you correctly, you're looking for this?

var tasks = new[] { task1, task2, task3 };
try { await Task.WhenAll(tasks); } catch (...) { }
return process(tasks);


来源:https://stackoverflow.com/questions/15857350/how-to-handle-partial-success-in-a-batch-of-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!