Quiz Answer: Watch out for the Eeeevil Thread.Abort.

Yesterday I posted a little quiz with an example of an HttpHandler implemented as an ASHX file.

Brad Wilson obviously knew the answer, but only gave a hint for others to elaborate on. BigJimSlade (no link given) expanded on the answer. BigJim, I have a GMail account for you if you want one.

Calling HttpResponse.Redirect(string url) actually calls an overload HttpResponse.Redirect(string url, bool endResponse) with endResponse set to true. If endResponse is set to true, HttpResponse.Redirect will make a call to HttpResponse.End().

That method in turn calls Thread.CurrentThread.Abort(). Oh the depravity! Once again, Thread.Abort rears its ugly head.

So as you see, the code sample will ALWAYS redirect to /default.aspx because the HandleRedirect method throws a ThreadAbortException every time. To fix this, I merely need to change the HandleRedirect method to call ctx.Response.Redirect("/special.aspx", false);.

The fact that this week seems to be “Thread.Abort Week” isn't why I posted this quiz. I ran into this problem the other day in my carelessness. It’s a result of my old ASP 3.0 habits resurfacing after years of suppressing them. It took me a few minutes to realize why my code never made it to special.aspx.

Technorati Tags: ,

What others have said

Requesting Gravatar... BigJimSlade Nov 18, 2004 1:17 PM
# re: Quiz Answer: Watch out for the Eeeevil Thread.Abort.
Aready have one... I will have to settle for the fame and noteriety
8)
Requesting Gravatar... Haacked Nov 18, 2004 2:12 PM
# re: Quiz Answer: Watch out for the Eeeevil Thread.Abort.
Excellent! Notoriety is a good thing. ;)
Requesting Gravatar... Joel Martinez Jan 29, 2005 7:02 AM
# re: Quiz Answer: Watch out for the Eeeevil Thread.Abort.
hey ... just wanted to let you know that this came up in a google search for the same issue. Thanks for posting it :-)
Requesting Gravatar... Haacked Jan 29, 2005 8:57 AM
# re: Quiz Answer: Watch out for the Eeeevil Thread.Abort.
Thanks Joel.
Requesting Gravatar... Ahmed Hafez May 09, 2005 8:17 AM
# re: Quiz Answer: Watch out for the Eeeevil Thread.Abort.
That is a good article. I was facing a problem in aborting a thread and this article was very helpful for me.
Requesting Gravatar... DOTNETDOMINATOR May 12, 2006 9:26 AM
# re: Quiz Answer: Watch out for the Eeeevil Thread.Abort.
Thanks,
Your Artical has still been helpful.. This has solved my problem. Great Artical.

What do you have to say?

(will show your gravatar)
Please add 8 and 2 and type the answer here: