1

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?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
GurdeepS
  • 65,107
  • 109
  • 251
  • 387

1 Answers1

-2

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
        }
    }
}
DavGarcia
  • 18,540
  • 14
  • 58
  • 96
  • Wouldn't you be clueless as to when that function was finished? – Joe Phillips Jun 11 '09 at 16:19
  • Yes, usually I'd use this to just call a long running process and forget about it. But if I needed to know when it was done, or if it even finished, the end of the process can update a status in a database. If the web page you are on needs to know when it is done, say a progress bar followed by a "Completed!" message, you can have myAsyncProcess update a Session object periodically along the way. Then use a client-side timer (ASP.NET AJAX Timer, for example) to call a function on the server to check the Session status. – DavGarcia Jun 13 '09 at 01:57
  • I used the your code but an exception is thrown: `Synchronous execution of an asynchronous task is not allowed` – Xaqron Jan 12 '11 at 06:01
  • What are you doing inside you task? Never seen that error before. Just to test, I created a new page and project, and it compiled then executed without errors. (See the updated answer for source code.) – DavGarcia Jan 13 '11 at 18:07