title: ASP.NET HTTPHandlers and long running processes
body: <p>This is a multi-part question.</p>
  
  <p>I have a process that can take several minutes to complete, it is ran by a calling a HTTPHandler using a asynchronous javascript request.</p>
  
  <p>Question 1: How can I ensure that this request does not time out on both the server and the client?</p>
  
  <p>Question 2: Is it possible to emit data from the HTTPHandler while processing that is sent back to the XmlHttpRequest object before the final page is completed?</p>
  
  <p>I'd like to calculate actual workload and return percentage done. I'm guessing that this is possible.</p>
  
  <p>Any tips?</p>
  
  <p>EDIT:</p>
  
  <p>A quick test:</p>
  
  <pre><code>    public void ProcessRequest(HttpContext context)
      {
          context.Response.ContentType = "text/plain";
          for (int i = 0; i &lt; 100; i++)
          {
              context.Response.Write("Hello World " + i + Environment.NewLine);
              System.Threading.Thread.Sleep(500);
              context.Response.Flush();
          }            
      }
  </code></pre>
  
  <p>Pushes Hello World bit by bit to the client...This works on a synchronious request, I'll see if a xmlHttpRequest gets a readystatechange event for each line.</p>
  
  <p>I've always avoiding multithreading on ASP.NET apps because you don't have control of when the working process will die, which will result in all of its spawned threads dieing.</p>
  
  <p>Instead, why re-invent a separate thread pool when IIS is already doing this for you.</p>