-3
我有一个将数据插入到数据库异步方法:等待完成异步方法
public async void InsertRoutes(ObservableCollection<RouteEO> routes)
{
connection.CreateTableAsync<RouteEO>().ContinueWith((result) =>
{
Debug.WriteLine("Routes table created");
foreach (var route in routes)
{
var query = connection.Table<RouteEO>().
Where(v => v.InspectionId == route.InspectionId && v.EO_id == route.EO_id);
query.ToListAsync().ContinueWith((t) =>
{
Debug.WriteLine("Route record inserted or updated");
if (t.Result.Any())
connection.UpdateAsync(route);
else
connection.InsertAsync(route);
});
}
});
}
我想打电话给它,并执行代码的下一行,只有当方法执行将完成:
sqlController.InsertInspections(DataController.InspectionList);
Debug.WriteLine("Done");
但是,当我启动此代码时,在“表创建”和“记录插入”消息之前,在调试窗口中收到“完成”消息。
为什么以及如何解决?
'异步void *仅支持*以支持异步UI事件ha ndlers。 (考虑它是一个“向后兼容的情况)。除了特定的用例外,***从不***使用'async void'(不仅因为它阻止了任务正确等待,而且还阻止了系统知道是否任务执行期间出错) – David 2014-09-26 15:19:18
[异步,等待和奇怪的结果]的可能重复(http://stackoverflow.com/questions/26062412/async-await-and-strange-result) – 2014-09-26 15:23:58