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.
Remember Me
Page rendered at Wednesday, October 08, 2008 3:01:29 AM (Pacific Standard Time, UTC-08:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.