title: How do I update the client with server side variables during a lengthy loop?
body: <p>I am trying to put a web interface on a lengthy server side process which should send regular progress\statistic reports to the client as the process is running.  How can I do this?  </p>
  
  <p>Here is what I have attempted so far. The session in the webmethod is null for as long as the loop is processing.  Once the loop is finished and you press the start button again, it is able to pick up the session value and populate the label.  How do I get this to send updates to the client while the process is running?</p>
  
  <p>I am using VS2012 and ASP.NET 4.5.</p>
  
  <p>EDIT: To be more specific, the problem occurs while the server is busy with the loop.  If I take the loop away and simply try to pull a variable value from the server at regular intervals then there is no problem.  Put that variable in a loop and try and fetch it at regular intervals and you'll see what the problem is, the code I have posted should clarify the issue if you run it.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Default.aspx</strong></p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ClientProgressTest.Default" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          function GetCount() {
  
          $.ajax({
              type: "POST",
              url: "Default.aspx/GetCount",
              contentType: "application/json; charset=utf-8",
              data: {},
              dataType: "json",
              success: function (data) {
                  lblCount.innerHTML = data.d;
              },
              error: function (result) {
                  alert(result.status + ' ' + result.statusText);
              }
          });
  
          setTimeout(GetCount, 5000);
      }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;label id="lblCount"&gt;&lt;/label&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;asp:Button ID="btnGetCount" runat="server" Text="Get Count" OnClientClick="GetCount();" OnClick="btnGetCount_Click" /&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Default.aspx.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace ClientProgressTest
  {
      public partial class Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
  
          [WebMethod(EnableSession = true)]
          public static string GetCount()
          {
              string count = null;
  
              if (HttpContext.Current.Session["count"] != null)
              {
                  count = HttpContext.Current.Session["count"].ToString();
              }
  
              return count;
          }
  
          protected void btnGetCount_Click(object sender, EventArgs e)
          {
              Session["count"] = null;
  
              for (int i = 0; i &lt; 10000000; i++)
              {
                  Session["count"] = i;
              }
          }
  }
  }
  </code></pre>