Parallel.Invoke - 异常处理 [英] Parallel.Invoke - Exception handling
问题描述
我的代码运行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
您必须尽早完成以下三件事:
- 安排检查您是否要提早退出的操作中。
- 在检测到错误并捕获<$时抛出异常c $ c> AggregateException as Jon的回答表示。
- 使用取消令牌。但是,这只有在您有机会检查其
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:
- 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.
- Throw an exception when you detect the error and catch an
AggregateException
as Jon's answer indicates. - 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屋!