Thread was being aborted

I am using Server.Transfer. Everything works fine, but exception log shows following exception.

System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.AbortInternal()
   at System.Threading.Thread.Abort(Object stateInfo)
   at System.Web.HttpResponse.End()
   at System.Web.HttpServerUtility.Transfer(String path, Boolean preserveForm)
   at System.Web.HttpServerUtility.Transfer(String path)

Any idea to avoid above exception.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This exception is throw by the call to Server.Transfer in order to halt the execution of the current method – exactly the same thing gets thrown if you do Response.Redirect.

The two choices you have are:

  • Catch and rethrow the
    ThreadAbortException / reperform the
    Server.Transfer
  • Make sure that you
    only do Server.Transfer in places
    where it wont be caught (recommended)

EDIT: Scratch that, http://support.microsoft.com/kb/312629 has a couple of other suggestions to try, but I still recommend #2 above.

Method 2

Another way to solve this, is to catch the generated error and to not rethrow it:

        catch (ThreadAbortException)
        { 
        }

Method 3

Caling Server.Transfer will call Response.End which always throws a ThreadAbortException. This is a “special” exception because while it can be caught in a catch block, it will always be re thrown at the end of the catch block. I would have your error logging ignore ThreadAbortExceptions.

Method 4

This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally.

Solution for this Problem is as follows.

For Server.Transfer, use the Server.Execute method instead.

Visit this link for download sample example. http://jayeshsorathia.blogspot.com/2012/03/thread-was-being-aborted-error-occured.html

Method 5

Replace Response.End() With HttpContext.Current.ApplicationInstance.CompleteRequest();

Method 6

Replacing Response.End() by the following helped fix the problem.

Response.Flush();
Response.Close();

Refer Can we use Response.Flush () instead of Response.End()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x