Threading in Web Applications

  • Thread starter Thread starter Jensen Carpenter, Mortician
  • Start date Start date
J

Jensen Carpenter, Mortician

If I put a ThreadStart on a ThreadPool, triggered by a button on a web
form, and I close the web form -- will the method on the Thread run to
completion?

Is it possible to build a web application, where, if I launch a thread,
and then close the web page, when I open the webform again, I could see
a result?
 
The short answer to your first question is that closing the client browser
window will not end the thread.

And to the second question, no, there is no way to open a new browser window
and see the result directly.

What you can do is store an identifying value in a cookie and the result of
your thread operation in some persistent format such as a database. Then,
when the new browser opens, you could identify the user by the cookie and
retrieve the value to provide that user from the database.

Semi-permanent storage such as Application variables will not work because
you can't be sure that the same process will still be running or will handle
the new request.

Here's an article to introduce you to threading in ASP.Net applications:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/DIforWC-CH01.asp

HTH

DalePres
MCAD, MCDBA, MCSE
 
DalePres said:
The short answer to your first question is that closing the client browser
window will not end the thread.

Great...so even if it's a long running process...and I put it on a
ThreadPool with no return value...it will complete...how about if it
does return a value...will it complete?

Can I see this in VS.NET 2003?
And to the second question, no, there is no way to open a new browser window
and see the result directly.

That's okay too. I will return a status to a database from the thread,
and then refresh the web form ( or it will just update the datagrid when
they log back in ).
 
DalePres said:
The short answer to your first question is that closing the client browser
window will not end the thread.

In VS.NET it looks like the threads end when I press stop.

Shouldn't they continue on ?

Are you sure they continue...
 
I don't think the web server has any knowledge of the client browser
pressing stop. However, I could be wrong.

It is an easy test though - just output some message into a file at the end
of the request and put a Thread.Sleep(1500) in there. That will cause it to
wait 1.5 seconds until it finishes - giving you time to hit stop. Then
check and see if the debug message was written.

Take care.
 
Yes, they end when you close down the browser in Visual Studio. But that is
simply slide of hand because closing the browser causes the debugger to
detach from the running process and unload the application domain. That
certainly does not happen in the real world. Visual studio plays certain
tricks. In the real world, the server knows nothing about the front end. It
receives a request, handles it, and disconnects. It could care less if the
requestor never showed up again - the web is stateless.

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
_________________________
 
Back
Top