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

0 comments suggest edit

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.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

6 responses

  1. Avatar for BigJimSlade
    BigJimSlade November 18th, 2004

    Aready have one... I will have to settle for the fame and noteriety

    8)

  2. Avatar for Haacked
    Haacked November 18th, 2004

    Excellent! Notoriety is a good thing. ;)

  3. Avatar for Joel Martinez
    Joel Martinez January 28th, 2005

    hey ... just wanted to let you know that this came up in a google search for the same issue. Thanks for posting it :-)

  4. Avatar for Haacked
    Haacked January 28th, 2005

    Thanks Joel.

  5. Avatar for Ahmed Hafez
    Ahmed Hafez May 8th, 2005

    That is a good article. I was facing a problem in aborting a thread and this article was very helpful for me.

  6. Avatar for DOTNETDOMINATOR
    DOTNETDOMINATOR May 11th, 2006

    Thanks,
    Your Artical has still been helpful.. This has solved my problem. Great Artical.