I am under the impression that the below code should work asynchronously.
However, when I am looking at firebug, I see the requests fired asynchronously, but the results coming back synchronously:
Controller:
[HandleError]
public class HomeController : Controller
{
public ActionResult Status()
{
return Content(Session["status"].ToString());
}
public ActionResult CreateSite()
{
Session["status"] += "Starting new site creation";
Thread.Sleep(20000); // Simulate long running task
Session["status"] += "<br />New site creation complete";
return Content(string.Empty);
}
}
Javascript/jQuery:
$(document).ready(function () {
$.ajax({
url: '/home/CreateSite',
async: true,
success: function () {
mynamespace.done = true;
}
});
setTimeout(mynamespace.getStatus, 2000);
});
var mynamespace = {
counter: 0,
done: false,
getStatus: function () {
$('#console').append('.');
if (mynamespace.counter == 4) {
mynamespace.counter = 0;
$.ajax({
url: '/home/Status',
success: function (data) {
$('#console').ht开发者_开发知识库ml(data);
}
});
}
if (!mynamespace.done) {
mynamespace.counter++;
setTimeout(mynamespace.getStatus, 500);
}
}
}
Addtional information:
- IIS 7.0
- Windows 2008 R2 Server
- Running in a VMWare virtual machine
Can anyone explain this? Shouldn't the Status
action be returning practically immediately instead of waiting for CreateSite
to finish?
Edit:
How can I get the long running process to kick off and still get status updates?
The web server only runs one request at a time from each user. The requests are queued based on the session id.
(This makes it a lot easier to build web applications, as you don't have to consider the possibility that two threads access the data for the same user at the same time.)
精彩评论