Parallel.Invoke - 异常处理 [英] Parallel.Invoke - Exception handling

查看:262
本文介绍了Parallel.Invoke - 异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码运行4函数来填充信息(使用Invoke)到类如:

  class Person 
{
int年龄
string name;
long ID;
bool isVegeterian

public static Person GetPerson(int LocalID)
{
person person;
Parallel.Invoke(()=> {GetAgeFromWebServiceX(person)},
()=> {GetNameFromWebServiceY(person)},
()=> {GetIDFromWebServiceZ }

)//连接到我的数据库并获取信息(如果使用LocalID)
....
if(! person.isVegetrian)
return null
....
});
}
}

我的问题是:不是一个vegeterian,但我想要能够停止所有线程,停止处理,只返回null。 c 要退出 Parallel.Invoke 您必须尽早完成以下三件事:


  1. 安排检查您是否要提早退出的操作中。

  2. 在检测到错误并捕获<$时抛出异常c $ c> AggregateException as Jon的回答表示。

  3. 使用取消令牌。但是,这只有在您有机会检查其 IsCancellationRequested 属性。

您的代码将如下所示:

  var cts = new CancellationTokenSource(); 
try
{
Parallel.Invoke(
new ParallelOptions {CancellationToken = cts.Token},
()=>
{
if(!person.IsVegetarian)
{
cts.Cancel();
throw new PersonIsNotVegetarianException();
}
},
> {GetAgeFromWebServiceX(person,cts.Token)},
()=> {GetNameFromWebServiceY(person,cts.Token)},
()=> {GetIDFromWebServiceZ(person,cts.Token )}
);
}
catch(AggregateException e)
{
var cause = e.InnerExceptions [0];
//检查原因是否为PersonIsNotVegetarianException。
}

但是,正如我说的,取消令牌只有当你可以检查它们中。因此,在 GetAgeFromWebServiceX 中应该有机会检查取消令牌并提前退出,否则,将令牌传递到这些方法是没有意义的。


My code runs 4 function to fill in information (Using Invoke) to a class such as:

class Person
{
    int Age;
    string name;
    long ID;
    bool isVegeterian

    public static Person GetPerson(int LocalID)
    {
        Person person;
        Parallel.Invoke(() => {GetAgeFromWebServiceX(person)}, 
                        () => {GetNameFromWebServiceY(person)},
                        () => {GetIDFromWebServiceZ(person)},
                        () => 
                        {
                           // connect to my database and get information if vegeterian (using LocalID)
                           ....
                           if (!person.isVegetrian)
                               return null
                           ....
                        });
     }
}

My question is: I can not return null if he's not a vegeterian, but I want to able to stop all threads, stop processing and just return null. How can it be achieved?

解决方案

To exit the Parallel.Invoke as early as possible you'd have to do three things:

  1. Schedule the action that detects whether you want to exit early as the first action. It's then scheduled sooner (maybe as first, but that's not guaranteed) so you'll know sooner whether you want to exit.
  2. Throw an exception when you detect the error and catch an AggregateException as Jon's answer indicates.
  3. Use cancellation tokens. However, this only makes sense if you have an opportunity to check their IsCancellationRequested property.

Your code would then look as follows:

var cts = new CancellationTokenSource();
try
{
    Parallel.Invoke(
        new ParallelOptions { CancellationToken = cts.Token },
        () =>
        {
            if (!person.IsVegetarian)
            {
                cts.Cancel();
                throw new PersonIsNotVegetarianException();
            }
        },
        () => { GetAgeFromWebServiceX(person, cts.Token) }, 
        () => { GetNameFromWebServiceY(person, cts.Token) },
        () => { GetIDFromWebServiceZ(person, cts.Token) }
    );
}
catch (AggregateException e)
{
    var cause = e.InnerExceptions[0];
    // Check if cause is a PersonIsNotVegetarianException.
}

However, as I said, cancellation tokens only make sense if you can check them. So there should be an opportunity inside GetAgeFromWebServiceX to check the cancellation token and exit early, otherwise, passing tokens to these methods doesn't make sense.

这篇关于Parallel.Invoke - 异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆