Search code examples
asp.netihttpasynchandler

How to use IHTTPAsyncHandler?


Imagine I have some code to read from the start to end of a file like so:

while(sw.readline != null)
{

}

I want this to be fully asynchronous. When deriving from IHTTPAsyncHandler, where would this code go and where would would the code to get the content of each line go?


Solution

  • I recently added some asynchronous processes to my ASP.NET pages in order to allow some long-running processes to take place while the user waited for the results. I found the IHTTPAsyncHandler quite inadequate. All it does is allow you to spin off a new thread while the page begins processing. You still have to make your own thread and create a AsyncRequestResult.

    Instead I ended up just using a normal thread in my codebehind instead, much more succinct:

    using System;
    using System.Web;
    using System.Threading;
    
    namespace Temp {
        public partial class thread : System.Web.UI.Page {
            protected void Page_Load(object sender, EventArgs e) {
                Thread thread = new Thread(new ThreadStart(myAsyncProcess));
                thread.Start();
            }
    
            private void myAsyncProcess() {
                // Long running process
            }
        }
    }