views:

280

answers:

2

In asp.net 2.0 , c#.

Is it possible to start a thread from a page, for example when a asp:button is clicked and from another page, check if that thread has exited?

Reason: We have a page that starts a long server-side script when a button is clicked. However, since we don't want to wait the ending of the script on that page, we thought about creating a thread that executes the script, then get the thread ID and redirect the user on a 'Please wait' page that refresh check every 5 seconds to check if the thread has exited.

Any better idea on how to achieve that?

Thank you!

A: 

This can be done, but I would use Ajax to make the experience better, however.

Basically, you would call your method and it would return the identifier for the thread. It doesn't have to be the thread id, but you need some sort of unique identifier that can be passed between pages/calls.

Before you return from that call/page, you would execute your thread.

Then, when your client checks on the status, it would check the session, or some other shared state, which would have the result keyed on the id you returned earlier.

If it finds a value corresponding to the key, then it displays the result (and removes the result from the shared state), otherwise, it checks again in a specified period of time.

casperOne
A: 

There is already an approach built into ASP.NET to do this sort of thing - it is the Asynchronous page model. There are 2 ways that this can be implemented and you can find details on them here with all the benefits explained.

Rob West