Question

I have an ASP.NET page that is taking too long to load. A little testing shows that the server side logging is taking a fair chunk of time and (because the user never needs to see the logging results) I want to delay it until after the page has loaded.

Is there a simple way to do this?

I've tried placing it in the page's Disposed event but that doesn't seem to fire and in the Unload event but that fires too soon. I'd rather not have to spawn a thread but I might be able to if that's what it takes.

I am NOT looking for AJAX. I want to do a normal full page load and then after the page has loaded (as seen from the client side) do a little more processing.

Was it helpful?

Solution

This appears to work:

protected void Page_Unload(object sender, EventArgs e)
{
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Close();
    Thread.Sleep(20000); // Do processing here instead of sleeping :)
    Debug.WriteLine("Done!");
}

You mentioned the Unload event fires too soon, but it looks to me that it's right on time. You just need to close the connection manually. Flushing seems to be mandatory as well, which is a little strange, as I thought a flush was implicit when you close.

There might be some downsides to this, as it keeps the process handling the request busy, unable to handle the next HTTP request. IIS7 has a configurable limit on the number of simultaneous requests however. You wanted simple, I think this is as simple as it will get.

OTHER TIPS

Have you considered creating a logging service that takes a queue of messages to log and just performs the logging in its own thread?

Using this, you can leave all the logging to occur where they were in the code before and just make asynchronous calls to the logging methods.

You are looking for an Asynchronous process. AJAX allows for this as well as other implementations. Do a search for Async and ASP.Net and you'll find a ton of resources.

Basically your web page kicks off and runs processes in a separate thread while the UI continues to render.

Try calling Flush on the response stream before you log. Unload isn't fired until after the entire response is sent. If you Flush() there and then log, it should be the last thing the page does.

The effect should be that the user sees the page, but that it isn't finished loading until the log is done. But they can interact with the page, so probably they won't care.

This is possible with asynchronous threads. We created a Page for a customer which checks every 2 minutes if there is an update and if so we updated the page.

Maybe this howto will help you:

http://www.eggheadcafe.com/articles/20060918.asp

hope could help you

Instead of logging it into your log, make a string out of it (using StringBuilder) and towards the end, push that entire string into Log.

You could lose the logging, if the page fails to load due to exception.
Pardon me, if I have not understood the problem correctly.

What kind of logging is it and where is it going?

One way may be to spin off another thread (you'd need to increase the amount of threads available to asp.net) and perform the logging in there. You could use the ParameterizedThreadStart and pass an object to be used for logging. (Don't pass a database connection/command object though, that'd be bad!)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top