【发布时间】:2019-06-19 19:51:49
【问题描述】:
我的服务器有一个长时间运行的 POST 操作
[HttpPost]
public async Task<ActionResult> LongOperation()
{
var s = (int)System.Web.HttpContext.Current.Session["p"];
IProgress<int> progress = new Progress<int>(value => s = value);
await Task.Run(() =>
{
Task.Delay(2000);
progress.Report(33);
})
.ContinueWith(prevTask =>
{
Task.Delay(2000);
progress.Report(66);
})
.ContinueWith(prevTask =>
{
Task.Delay(2000);
progress.Report(100);
});
return View("Index", new TempModel());
}
我正在尝试通过 GET 方法监控其进度
public ContentResult GetProgress()
{
var session = (int)System.Web.HttpContext.Current.Session["p"];
return Content(session.ToString());
}
我的 jQuery 调用了初始 POST 请求..
this.Submit = function(){
$.post("@Url.Action("LongOperation", "MyController")", function (response) {
}
}
...并定期监控进度
this.PollServerForProgress = function PollServerForProgress() {
$.get("@Url.Action("GetProgress", "MyController")", function (response) {
console.log(response);
setTimeout(that.PollServerForProgress, 100);
}
})
}
但是,Submit 和 PollServerForProgress 似乎是按顺序发生的。
我总是从 PollServerForProgress 得到响应为 1 -- iniial value 或 100 -- final value
不像我期望的那样1, 33, 66, 100。
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 async-await settimeout polling