Monday, February 25, 2008

ThreadAbortException while using Response.End

Whenever there is a call to Response.End() method in your ASP.NET code, and there are lines of code following this call (which we expect not to be executed), then in such a case a System.Threading.ThreadAbortException is thrown which can be caught using a try-catch block.

This exception can also be thrown while calling Response.Redirect or Server.Transfer methods as they also call Response.End internally.

The resolution for this issue is -

  • Response.End : use HttpContext.Current.ApplicationInstance.CompleteRequest instead of Response.End.
  • Response.Redirect : use the overload Response.Redirect(String url, bool endResponse) where second parameter indicates if Response.End should be called or not.
  • Server.Transfer : use Server.Execute

While the requirement can be different and you may not want to just replace Server.Transfer with Server.Execute, handling the exception is a better solution in such a case. Also, calling ApplicationInstance.CompleteRequest or Response.Redirect with endResponse set to false, will still go through the complete page and application life cycle which may be expensive. In this case as well, when you may want the execution to be aborted immediately, instead of workarounding this issue as mentioned above, handle the ThreadAbortException and let all threads be aborted immediately.

This issue is documented in Microsoft's KB at http://support.microsoft.com/kb/312629/EN-US/ . It states, it applies to ASP.NET 1.0 and 1.1, but it applies to 2.0 as well.

Related Articles :

8 comments:

Anonymous said...

Asking questions are really good thing if
you are not understanding something entirely, however this
post presents good understanding yet.
Feel free to visit my web-site ; porn

Anonymous said...

Everything is very open with a clear explanation of the challenges.
It was truly informative. Your site is extremely helpful.

Thanks for sharing!
Also visit my web site porn

Anonymous said...

Pretty! This was an incredibly wonderful post. Thanks for
providing this info.
Also visit my web page : porn movies

Anonymous said...

you are truly a good webmaster. The website loading velocity is incredible.
It seems that you're doing any unique trick. Moreover, The contents are masterwork. you have performed a magnificent job on this topic!
Here is my website Mic / Microphone

Anonymous said...

Hi there! I could have sworn I've been to this site before but after browsing through a few of the articles I realized it's new to me.
Nonetheless, I'm certainly pleased I stumbled upon it and I'll
be bookmarking it and checking back frequently!
Also visit my web site ... Las Vegas

Anonymous said...

A motivating discussion is worth comment. I believe that you need to publish
more about this subject, it may not be a taboo subject but usually people don't speak about these issues. To the next! All the best!!

My site Green tea weight loss
My site :: Green tea weight loss

Anonymous said...

Nice blog right here! Additionally your website quite a bit up very
fast! What host are you the use of? Can I am getting
your affiliate hyperlink on your host? I wish my website loaded up as quickly as yours lol

Also visit my web-site; Summitt Energy Solutions

Anonymous said...

Thank you for the good writeup. It actually was a amusement account it.
Glance complicated to far delivered agreeable from you!
By the way, how could we communicate?

Feel free to visit my web blog: webcam girls