Controller actions seem synchronous, albeit on different requests?

I get the impression that the code below should work asynchronously.

However, when I look at firebug, I see that the requests are started asynchronously, but the results are returned synchronously:

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

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

Additional Information:

  • IIS 7.0
  • Windows 2008 R2 Server
  • Running on VMWare Virtual Machine

Can anyone explain this? Should the action Statusreturn almost immediately, and not wait for completion CreateSite?


Edit:

How can I start a lengthy process and start getting status updates?

+3
source share
1 answer

- . .

( -, .)

+2

Source: https://habr.com/ru/post/1785205/


All Articles