Thread was being aborted
Asked Answered
Y

6

33

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.

Ybarra answered 16/9, 2009 at 14:43 Comment(0)
Z
41

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.

Zenobia answered 16/9, 2009 at 14:49 Comment(0)
C
11

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

        catch (ThreadAbortException)
        { 
        }
Candelabrum answered 15/1, 2013 at 8:2 Comment(2)
empty catches are badArnoldoarnon
@Rob, usually, but not always.Paper
P
8

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.

Prefatory answered 16/9, 2009 at 14:50 Comment(3)
It make sense to me that Server.Transfer will call Response.End implicitly. What do you mean by error logging ignore threadAbortException.Ybarra
If you are logging exceptions to a persistable logging system, I would filter out ThreadAbortExceptions or have your reports that query this loging system filter them out.Prefatory
It make sense that this exception is not serious one, therefore I can ignore it by filtering out.. thank you.Ybarra
A
8

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.

Adrienadriena answered 23/4, 2012 at 9:52 Comment(0)
D
1

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

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

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

Deerhound answered 11/8, 2016 at 18:15 Comment(0)
G
1

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

Granulation answered 11/2, 2017 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.