在metro应用程序中,我需要执行许多WCF调用.有大量的调用,所以我需要在并行循环中进行调用.问题是并行循环在WCF调用完成之前退出.
你会如何重构这个按预期工作?
var ids = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
var customers = new System.Collections.Concurrent.BlockingCollection<Customer>();
Parallel.ForEach(ids, async i =>
{
ICustomerRepo repo = new CustomerRepo();
var cust = await repo.GetCustomer(i);
customers.Add(cust);
});
foreach ( var customer in customers )
{
Console.WriteLine(customer.ID);
}
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud) 当我们启动多个异步实体框架查询并并行运行时会发生什么?
他们是否并行执行?它们是否由实体框架序列化?这不受支持吗?它会导致异常吗?
public async Task QueryDatabase()
{
using (var context = new MyDbContext())
{
Task task1 = context.SomeTable1.ToListAsync();
Task task2 = context.SomeTable2.ToListAsync();
await Task.WhenAll(task1, task2);
}
}
Run Code Online (Sandbox Code Playgroud) 如果我的WinForms应用程序启动任务以在执行任务时保持响应,我在处理AggregateException时会遇到问题。
简化的情况如下。假设我的Form有一个相当慢的方法,例如:
private double SlowDivision(double a, double b)
{
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));
if (b==0) throw new ArgumentException("b");
return a / b;
}
Run Code Online (Sandbox Code Playgroud)
按下按钮后,我希望我的表单显示SlowDivision(3,4)的结果。以下代码会将用户界面挂起一段时间:
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = this.SlowDivision(3, 4).ToString();
}
Run Code Online (Sandbox Code Playgroud)
因此,我想开始执行处理任务。该任务完成后,应继续执行将显示结果的操作。为了防止InvalidOperationException,我需要确保从创建标签的线程访问label1,因此需要Control.Invoke:
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew ( () =>
{
return this.SlowDivision(3, 4);
})
.ContinueWith( (t) =>
{
this.Invoke( new MethodInvoker(() =>
{
this.label1.Text = t.Result.ToString();
}));
});
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好,但是如何处理异常,例如,如果我想计算SlowDivision(3,0)?
通常,如果任务抛出未处理的异常,它将通过AggregateException转发到等待线程。许多示例显示以下代码:
var myTask = Task.Factory.StartNew ( () => ...);
try
{
myTask.Wait();
}
catch …Run Code Online (Sandbox Code Playgroud)