Controller actions appear to be synchronous though on different requests?

Posted by Oded on Stack Overflow See other posts from Stack Overflow or by Oded
Published on 2011-01-13T13:47:12Z Indexed on 2011/01/13 13:53 UTC
Read the original article Hit count: 218

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:

Requests are

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').html(data);
                }
            });
        }

        if (!mynamespace.done) {
            mynamespace.counter++;
            setTimeout(mynamespace.getStatus, 500);
        }
    }
}

Addtional information:

  • IIS 7.0
  • Windows 2008 R2 Server
  • Running in a VMWare virutual 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?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX