简体   繁体   中英

How to delay a controller action result in ASP.Net MVC3 C#4

Im an developing a web application with MVC3, Razor, C# 4, jQuery.

Within one screen (page) I do AJAX calls to a controller action to get some screen-updates. Using Javascripts setTimeout() I do polling.

To optimize this polling in case where the server does not have screen-updates I like to delay the HTTP response and wait a little until I a) get screen-updates or b) hit some timeout (eg. 10 seconds or so).

I tried to do something like this

[OutputCache(Duration = 0)]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult CheckForUpdates()
{
    var startTime = DateTime.Now;
    while (_haveUpdates || DateTime.Now > startTime.AddSeconds(10   ))
    {
        Thread.Sleep(3);
    }

    return _haveUpdate ? Json(new {updates = ... }, JsonRequestBehavior.AllowGet) : null;
}

In the view I use Javascript / jQuery like this:

<script>
$(function () {
    scheduleNextCheck();
});

function scheduleNextCheck() {
    setTimeout(function() {
        $.ajax({
            url: "@Url.Action("CheckForUpdates")",
            dataType: 'json',
            data: null,
            timeout: 10000,
            cache: false,
            success: function(data) {
                if (data != null && data.updates != null ) {
                    // Apply Updates
                } else {
                    scheduleNextCheck();
                }
            },
            error: function() {
                scheduleNextCheck();
            }
        });
    }, 2000);
}

When using this code the IIS7 worker process will hang/freeze totally so only killing the worker-process can unlock the entire IIS7 server. So Thread.Sleep() seems not to be a good idea. At least not my applications environment.

Did anybody do timething like this with ASP.Net MVC yet or have any idea?

Thx, Marc

==== Update: ====

Found one of the problems: the while criteria was wrong. It should be like this:

    while (noUdates && DateTime.Now < startTime.AddSeconds(10))

The problem now is that other AJAX-requests are canceled while this sleep-delay-loop is running.

About SignalR: Have to take a closer look but the problem is that this needs additional server- and client-side libraries which I have to get approval for so I was hoping for an "easy" small solution with less impact and less need for training. But SignalR still is an option for one of the next releases to replace polling-stuff - but need to get some training and experience with this stuff first.

==== Update 2: ====

I am looking for a solutions that works without additional libraries / frameworks so I can apply it to the nearly finished web-application without a huge impact.

You may take a look at SignalR which is designed exactly for such situations. Instead of having the client poll the server with multiple HTTP requests, it is the server that notifies the client when updates are available.

I'm also looking into SignalR with my site, I'm reading this article which is pretty good: http://www.dotnetcurry.com/ShowArticle.aspx?ID=780 .

From my understanding, the only real constraint is the browser version, but SignalR degrades to accommodate older versions.

There's some info here too: http://www.entechsolutions.com/browser-alerts-with-asp-net-4-5-and-signalr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM