c# - Task WaitAll 在 C# 中无法正常工作,用于 DB 任务
问题描述
我在 Northwind DB 上玩了一下异步 LINQ,但遇到了Task.WaitAll(task1, task2)
. 下面是我从中调用的方法static void Main(string[] args)
。
public static void PerformDatabaseOperations()
{
using (var ne = new NORTHWNDEntities())
{
try
{
var aup = ne.Products.AverageAsync(p => p.UnitPrice)
.ContinueWith(t => Console.WriteLine($"Average unit price is {t.Result}"));
var ao = ne.Orders.GroupBy(o => o.OrderDate).AverageAsync(group => (double)group.Count())
.ContinueWith(t => Console.WriteLine($"Average orders per day is {t.Result}"));
Task.WaitAll(aup, ao);
}
catch (AggregateException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
当我运行它时,这会引发 AggregateException:
System.AggregateException: One or more errors occurred. --->
System.AggregateException: One or more errors occurred. --->
System.NotSupportedException: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.
这种方法有什么我遗漏的吗?我很感激你的任何暗示。
解决方案
DbContext 不是线程安全的 - 您不能在 2 个线程中使用相同的实例。
按照异常的建议,只需将您的代码更改为:
public static async Task PerformDatabaseOperations()
{
using (var ne = new NORTHWNDEntities())
{
try
{
var t = await ne.Products.AverageAsync(p => p.UnitPrice);
Console.WriteLine($"Average unit price is {t}");
var ao = await ne.Orders.GroupBy(o => o.OrderDate).AverageAsync(group => (double)group.Count());
Console.WriteLine($"Average orders per day is {ao}");
}
catch (AggregateException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
注意async Task
方法定义。
如果你真的想同时执行两个查询,每个任务都需要自己的 DbContext 实例。
推荐阅读
- laravel - 拉拉维尔 5.5。[Route: password.request] 缺少必需的参数
- java-8 - 如何使用 Nashorn 在 Java8 中执行 JavaScript 文件
- angularjs - 在 Cloud Foundry 上部署 Angular/ionic 应用程序
- shell - 使用 shell 提取行并保存到多个 csv 文件
- python - mako 访问代码块外的变量
- html - HTML:在输入字段中显示键
- sql - SQL选择排序组合列
- swift - 如果“UITextField”中的文本有效,如何创建“RxAction”以启用/禁用按钮?
- r - 在 sf 对象上设置正确的 crs 以绘制坐标点
- java - 如何将我的二维数组填充为相同的值