我刚开始在我的项目中使用AsyncController来处理一些长时间运行的报告.因为我可以启动报告,然后在等待它返回并在屏幕上填充元素时执行一些其他操作,这似乎是理想的.
我的控制器看起来有点像这样.我试图使用一个线程执行长任务,我希望释放控制器以获取更多请求:
public class ReportsController : AsyncController
{
public void LongRunningActionAsync()
{
AsyncManager.OutstandingOperations.Increment();
var newThread = new Thread(LongTask);
newThread.Start();
}
private void LongTask()
{
// Do something that takes a really long time
//.......
AsyncManager.OutstandingOperations.Decrement();
}
public ActionResult LongRunningActionCompleted(string message)
{
// Set some data up on the view or something...
return View();
}
public JsonResult AnotherControllerAction()
{
// Do a quick task...
return Json("...");
}
}
Run Code Online (Sandbox Code Playgroud)
但我发现的是,当我使用jQuery ajax请求调用LongRunningAction时,我之后做的任何进一步的请求都会在它后面备份,直到LongRunningAction完成才会处理.例如,调用LongRunningAction需要10秒,然后调用不到一秒的AnotherControllerAction.AnotherControllerAction只是在返回结果之前等待LongRunningAction完成.
我还检查了jQuery代码,但如果我专门设置"async:true",这仍然会发生:
$.ajax({
async: true,
type: "POST",
url: "/Reports.aspx/LongRunningAction",
dataType: "html",
success: …Run Code Online (Sandbox Code Playgroud)