title: How to delay a controller action result in ASP.Net MVC3 C#4
body: <p>Im an developing a web application with MVC3, Razor, C# 4, jQuery.</p>
  
  <p>Within one screen (page) I do AJAX calls to a controller action to get some screen-updates. Using Javascripts setTimeout() I do polling.</p>
  
  <p>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).</p>
  
  <p>I tried to do something like this</p>
  
  <pre><code>[OutputCache(Duration = 0)]
  [AcceptVerbs(HttpVerbs.Get)]
  public ActionResult CheckForUpdates()
  {
      var startTime = DateTime.Now;
      while (_haveUpdates || DateTime.Now &gt; startTime.AddSeconds(10   ))
      {
          Thread.Sleep(3);
      }
  
      return _haveUpdate ? Json(new {updates = ... }, JsonRequestBehavior.AllowGet) : null;
  }
  </code></pre>
  
  <p>In the view I use Javascript / jQuery like this:</p>
  
  <pre><code>&lt;script&gt;
  $(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 &amp;&amp; data.updates != null ) {
                      // Apply Updates
                  } else {
                      scheduleNextCheck();
                  }
              },
              error: function() {
                  scheduleNextCheck();
              }
          });
      }, 2000);
  }
  </code></pre>
  
  <p></p>
  
  <p>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.</p>
  
  <p>Did anybody do timething like this with ASP.Net MVC yet or have any idea?</p>
  
  <p>Thx, Marc</p>
  
  <p><strong>==== Update: ====</strong></p>
  
  <p>Found one of the problems: the while criteria was wrong. It should be like this:</p>
  
  <pre><code>    while (noUdates &amp;&amp; DateTime.Now &lt; startTime.AddSeconds(10))
  </code></pre>
  
  <p>The problem now is that other AJAX-requests are canceled while this sleep-delay-loop is running.</p>
  
  <p><strong>About SignalR:</strong> 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.</p>
  
  <p><strong>==== Update 2: ====</strong></p>
  
  <p>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.</p>