Azure移动服务客户端 - 在多个InsertAsync任务上使用WaitAll

时间:2013-09-15 23:26:54

标签: c# azure insert async-await wait

我希望能够启动多个InsertAsync操作,然后等到完成。 [形式]形式的东西

Task[] tasks = new Task[10];
try
{
    IMobileServiceTable<Record> table = mobileService.GetTable<Record>();
    for (int i = 0; i < 10; i++)
    {
        Record rcd = new Record();
        tasks[i] = table.InsertAsync(rcd);
    }
}
catch { Assert.fail(); };
try
{
    Task.WaitAll(tasks, 10000);
    System.Diagnostics.Debug.WriteLine("WaitAll() has not thrown exceptions.");
}
catch { Assert.fail(); };

WaitAll刚刚超时,当我查看tasks数组中的任务时,没有一个正在运行。所以显然我使用了错误的模式,但看不到哪里。 任何见解都非常感谢!

1 个答案:

答案 0 :(得分:0)

行;解决了它。仍然不确定为什么。
我在问题中提出的原始代码是在异步例程中;我们称之为insertAsync()。 它是同步调用的,因为我不希望调用者继续执行,直到insertAsync完成 我之前写过的是

Task<bool> t = insertAsync();  
t.Wait();  

但是,如果你这样做,一切都会挂起 - 即没有任何表格插页开始 注释掉t.Wait(),一切正常。