我创建了两个ajax请求:一个执行任务(需要更长时间),另一个检查流程是否完成。
setInterval(function () {
$.ajax({
url: '/dojob',
success: function (resp) {
addToLog(resp.length ? resp : "Ok");
},
error: function (xhr) {
addToLog(xhr.responseText);
}
});
}, 2000);
setInterval(function () {
$.ajax({
url: '/checkstatus',
success: function (resp) {
addToLog(resp);
},
error: function (xhr) {
addToLog(xhr.responseText);
}
})
}, 2000);
这些动作看起来像:
private static readonly object obj = new object();
private static bool isBusy = false;
[HttpGet]
public IActionResult DoJob()
{
lock (obj)
{
isBusy = true;
}
// does long job
Thread.Sleep(6000);
lock (obj)
{
isBusy = false;
}
return Ok();
}
[HttpGet]
public IActionResult CheckStatus()
{
return Ok(isBusy);
}
这个想法是,在某一点上,
isBusy
如何正确获得
我很忙
什么时候打电话
CheckStatus