Wednesday, August 25, 2004

Sheesh another nerd post.  I thought this might be useful information for the community.

If you're using multiple threads in the execution of an ASP.net page, don't count on Response.End() to do what you think it might do.  Looking back it's pretty intuitive, I just didn't think about it.

At work, we have some pages that can take several minutes to execute.  They can pull millions of rows from the database and process them and do all kinds of craziness.  As a result, users don't like to sit around and watch Internet Explorer's progress bar decieve them time and again.  I implemented a progress mechanism to let the user know what is happening.  I went through several iterations before I come up with something really easy to work with.  It's completely transparent to the developer, and allows them to “push” progress messages onto a stack (using the wonderful “using“ statement), which is rendered for the user at an interval during the lifetime of a page.  It preserves the behavior of postbacks and Server.Transfers, but does break redirects if they happen after the progress gets started (which I hardly ever do).

Anyway, the point of the story is to tell you about Response.End.  The progress updating happens on a worker thread from the ThreadPool.  When I implemented it, I thought it would be nice to detect if a user has closed the browser or hit stop.  Then we could save some resources if no one is listening anymore.  So, before updating the progress, I do a Response.IsClientConnected, and if they aren't, I was calling Response.End().  This seemed to work at first, because it does do something, the problem is it doesn't stop what's going on in the main thread.

Reflector (the most useful CLR tool ever) reveals that Response.End() is basically just calling Thread.CurrentThread.Abort(), along with doing some clean-up and such.  Notice the CurrentThread...oops.  All I was doing was ending my progress rendering thread.  Like I said, it makes perfect sense now.

So, what do you do about it?  Well, I'll come up with something.  It shouldn't be too hard, but it will be pretty specific to my situation, so there's no real universal answer, unless Microsoft were to change the implementation so that it was keeping track of the threads and killing the main one, although then my worker threads wouldn't die.  Chances are if you've implemented some kind of multi-threading in your ASP.net pages, you can figure it out.

posted on Wednesday, August 25, 2004 2:06:47 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]
Related posts:
LinqToStdf now on CodePlex
Image Slicer for Deep Zoom in Silverlight 2
Silverlight limitations and Constrained Callvirt in IL
What are the generic Delegates in the framework for?
What is System.__Canon and why is it on my stack?
Adding support for .ashx-based views in ASP.net MVC
Tracked by:
"Canceling long-running ASP.net requests from a separate thread" (marklio) [Trackback]
"Careers in Psychology" (Careers in Psychology) [Trackback]
"cursos formacion barcelona" (cursos formacion barcelona) [Trackback]
"blood presure monitor" (blood presure monitor) [Trackback]
"piu carino asiatiche strip" (piu carino asiatiche strip) [Trackback]
"Residential Garage Doors" (Residential Garage Doors) [Trackback]
"used car under 5000" (used car under 5000) [Trackback]
"caroline county public library" (caroline county public library) [Trackback]
"motorola australia" (motorola australia) [Trackback]
"colleges escorts state college pa" (colleges escorts state college pa) [Trackback]
"brain teasers ice breakers" (brain teasers ice breakers) [Trackback]
"creatine facts" (creatine facts) [Trackback]
"win xp pro key" (win xp pro key) [Trackback]
"special window envelopes" (special window envelopes) [Trackback]
"girls sleeping hardcore" (girls sleeping hardcore) [Trackback]
"bridal lingerie and headpieces" (bridal lingerie and headpieces) [Trackback]
"Hilton embassy row" (Hilton embassy row) [Trackback]
"how to mix protonix drip" (how to mix protonix drip) [Trackback]
"composite video cable" (composite video cable) [Trackback]
"cardiofrequenzimetri polar" (cardiofrequenzimetri polar) [Trackback]
"christian single dating" (christian single dating) [Trackback]
"diploma frame" (diploma frame) [Trackback]
"cazzo in gola" (cazzo in gola) [Trackback]
"65 lincoln continental" (65 lincoln continental) [Trackback]
"motorcycle leather bags" (motorcycle leather bags) [Trackback]
"Kitchen Laminate Countertop" (Kitchen Laminate Countertop) [Trackback]
"fibromyalgia mould allergies" (fibromyalgia mould allergies) [Trackback]
"mlm genealogy report list" (mlm genealogy report list) [Trackback]
"pittsburgh entertainment" (pittsburgh entertainment) [Trackback]
"Contextualization Cues" (Contextualization Cues) [Trackback]
"lucas baby" (lucas baby) [Trackback]
"naked gun" (naked gun) [Trackback]
"Equipment Leasing Association" (Equipment Leasing Association) [Trackback]
"strap on girls" (strap on girls) [Trackback]
"alice tv" (alice tv) [Trackback]
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):