tags:

views:

85

answers:

2

Is it possible to return the page response to the user, before you've finished all your server side work?

Ie, I've got a cheap hosting account with No database, but I'd like to log a certain event, by calling a webservice on my other, more expensive hosting account (ie, a very slow logging operation)

I don't really want the user to have to wait for this slow logging operation to complete before their page is rendered.

Would I need to spin up a new thread, or make an asynchronous call? Or is it possible to return the page, and then continue working happily in the same thread/code?

Using ASP.Net (webforms) C# .Net 2.0 etc.

A: 

You sure can - try Response.Flush.

That being said - creating an asynchronous call may be the best way to do what you want to do. Response.Flush simply flushed the output buffer to the client, an asynchronous call would allow you to fire off a logging call and not have it impact the client's load time.

Keep in mind that an asynchronous call made during the page's life cycle in ASP.NET may not return in time for you to do anything with the response.

Andrew Hare
Wow! fast response (and Marc too). The logging would be pretty much fire and forget. I'll give both of these methods a try and see how my milage varies! Ta
Andrew M
+1  A: 

You would probably need a second thread. An easy option would be to use the ThreadPool, but in a more sophisticated setup a producer/consumer queue would work well.

At the simplest level:

    ThreadPool.QueueUserWorkItem(delegate {
        DoLogging(state details);
    });
Marc Gravell