【发布时间】:2015-06-14 13:18:15
【问题描述】:
我想在 C# 中执行一个异步数据库查询,该查询调用备份的存储过程。由于我们使用 Azure,这大约需要 2 分钟,我们不希望用户等待那么久。
所以想法是让它异步,以便任务在请求之后继续运行。
[HttpPost]
public ActionResult Create(Snapshot snapshot)
{
db.Database.CommandTimeout = 7200;
Task.Run(() => db.Database.ExecuteSqlCommandAsync("EXEC PerformSnapshot @User = '" + CurrentUser.AccountName + "', @Comment = '" + snapshot.Comment + "';"));
this.ShowUserMessage("Your snapshot has been created.");
return this.RedirectToActionImpl("Index", "Snapshots", new System.Web.Routing.RouteValueDictionary());
}
恐怕我还没有理解异步任务的概念。如果我不使用等待语句,查询将不会被执行(或中止?)。但实际上“等待”是我特别不想在这里做的一件事。
那么...为什么我不得不在这里使用等待?
或者该方法会被启动,但如果请求完成则被杀死?
【问题讨论】:
标签: c# asp.net entity-framework tsql asynchronous