开发者

Controller actions appear to be synchronous though on different requests?

开发者 https://www.devze.com 2023-02-03 20:50 出处:网络
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:

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 actions appear to be synchronous though on different requests?

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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消