Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果当前任务失败,如何继续执行队列中的其他任务?_C#_Multithreading_Task Parallel Library_System.reactive_Producer Consumer - Fatal编程技术网

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的答复,并尝试捕捉!我最终还是这样做了,没有时间更新这篇文章。