【发布时间】:2012-11-28 11:02:19
【问题描述】:
我有以下任务要完成:
在我的程序中有一组数据库查询或问题。 我想执行这些问题并等待所有问题的结果,或者如果一个问题失败则捕获错误!
我的 Question 对象如下所示(简化):
public class DbQuestion(String sql)
{
[...]
}
[...]
//The answer is just a holder for custom data...
public void SetAnswer(DbAnswer answer)
{
//Store the answer in the question and fire a event to the listeners
this.OnAnswered(EventArgs.Empty);
}
[...]
public void SetError()
{
//Signal an Error in this query!
this.OnError(EventArgs.Empty);
}
因此,向数据库发出的每个问题都有一个等待解析结果的侦听器。
现在我想触发一些与数据库异步的问题(最多 5 个左右),并使用来自所有问题的数据触发一个事件,如果只有一个问题抛出一个问题,则触发一个错误!
完成这项任务的最佳或好方法是什么? 我真的可以并行执行一个以上的问题并在一个问题引发错误时停止我的所有工作吗?
我想我需要一些灵感……
请注意:我正在使用 .NET 框架 2.0
【问题讨论】:
-
尝试使用 executeBatch()。这个可能有帮助codeproject.com/Articles/306722/…
标签: c# database asynchronous thread-safety