views:

406

answers:

1

I have a situation where user requests to do a long running process. I planing to start it on another thread in code-behind and return message to user "processing". Then every couple of seconds or so I will poll(via asynch call) the thread for its status. When thread is done I want to show success message.

Question, how do I poll a thread once the original request is complete?

As far as I understand, Page object will spin off a separate thread. When request is complete, the Page object is no longer there...but what about that thread? How do I get the status of that thread that I created?

+2  A: 

Well, you'll need some way of identifying the request. If you want to be able to scale this out to multiple machines, you should look at storing the request ID and current status in a database (or some similarly accessible-from-anywhere storage).

If you don't care about that situation and you're not worried about AppDomain recycling, you could just have a singleton dictionary from request ID to status - but consider making it automatically flush itself after some period if the client just goes away and doesn't check the status.

(Of course it doesn't have to be a true singleton - it could be some shared resource provided with dependency injection, but the point is it needs to be shared somehow so you can get at it from the new request.)

You could also potentially use a session-backed object - again with caveats about multi-server web sites and AppDomain recycling.

Jon Skeet
so, i create a singleton class that stores (request id, status) on original request. then the thread is going to update the status by using that singleton class. then on any subsequent request I can check status via that singleton class. is that correct?
dev.e.loper
The singleton would store a collection of "current request, status" yes. But you may well be better off just going for a session object, thinking about it. All that does is treat the session container in the framework as the singleton - it's exactly the same principle. You'll be able to see what's going on more clearly using your own container though.
Jon Skeet
as far as clean up, i can remove the the "current request, status" pair from session when user requests status and when status is "done". however, i'll still need a way to handle situation where user does not request for status (leaves or closes page). is there a standard way to do that clean up?
dev.e.loper
Well, if the session has a timeout associated with it, then the whole session will be removed after that period, I'd imagine. I'm not an ASP.NET expert though.
Jon Skeet