C# 如果当前任务失败,如何继续执行队列中的其他任务?
我有一些代码执行如下队列C# 如果当前任务失败,如何继续执行队列中的其他任务?,c#,multithreading,task-parallel-library,system.reactive,producer-consumer,C#,Multithreading,Task Parallel Library,System.reactive,Producer Consumer,我有一些代码执行如下队列 Queue .GetConsumingEnumerable() .ToObservable() .Select(x => x.ObserveOn(NewThreadScheduler.Default)) .SubscribeOn(NewThreadScheduler.Default) .Subscribe( grp => grp.ForEachAsync(b => b.Execute())
Queue
.GetConsumingEnumerable()
.ToObservable()
.Select(x => x.ObserveOn(NewThreadScheduler.Default))
.SubscribeOn(NewThreadScheduler.Default)
.Subscribe(
grp => grp.ForEachAsync(b => b.Execute())
.ContinueWith(ExecuteOnTaskFailure, TaskContinuationOptions.OnlyOnFaulted));
ExecuteOnTaskFailure方法定义如下
private static void ExecuteOnTaskFailure(Task previousTask)
{
if (!previousTask.IsFaulted)
return;
if (previousTask.Exception != null && previousTask.Exception.InnerExceptions != null)
foreach (var exception in previousTask.Exception.InnerExceptions)
{
Logger.Error("Task failed continued to the next task : " + exception.Message, exception);
}
}
这不管用。我似乎不知道如何使队列中的任务继续执行,即使其中一个任务无法执行。还有什么方法可以让我在队列结束时重新查询这个失败的任务
非常感谢您提供的任何帮助。听起来您想要的是在循环中尝试
-捕获:
grp.ForEachAsync(async b =>
{
try
{
await b.Execute();
}
catch (Exception ex)
{
Logger.Error(ex);
Queue.Add(b);
}
})
但是BlockingCollection
不是非常异步友好的,因为它在为空时阻塞(顾名思义)。你可能想考虑一种不同的方法。< /p> + 1的答复,并尝试捕捉!我最终还是这样做了,没有时间更新这篇文章。