ASP.NET exception “Thread was being aborted” causes method to exit

In the code below, sometimes someFunctionCall() generates an exception:

Thread was being aborted.

How come the code in code Block B never runs? Does ASP.NET start a new thread for each method call? I was suprised to see that when this exception happens the code in Block B never runs, the method returns, and my application keeps running. Can someone please explain this?

public void method()
{
     // CODE BLOCK A
     //...    

     try 
     {
         someFunctionCall(); // this call is generating thread abort exception
     }
     catch(Exception ex)
     {
         // log exception message
     }

    // CODE BLOCK B
    // ...    
}

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 is a ThreadAbortException; it’s a special exception that is automatically rethrown at the end of every catch block, unless you call Thread.ResetAbort().

ASP .Net methods like Response.End or Response.Redirect (unless you pass false) throw this exception to end processing of the current page; your someFunctionCall() is probably calling one of those methods.

ASP .Net itself handles this exception and calls ResetAbort to continue processing.

Method 2

To work around this problem, use one of the following methods:
For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event.

For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End. For example:

Response.Redirect ("nextpage.aspx", false);

If you use this workaround, the code that follows Response.Redirect is executed.
For Server.Transfer, use the Server.Execute method instead.

Method 3

What worked for me is increasing the “Idle Time-out (minutes)” property for the Application Pool on IIS.

I set it to “43200” which is the max value.

I also adjusted the recycling settings …

Method 4

Try this way :

This is my extension method :

 public static void WriteJSONObject(this HttpResponse response, object content) {
            response.ContentType = "application/json";
            response.Write(new JavaScriptSerializer().Serialize(content));
            response.End();

 }

And the logic :

public void RegisterUser() {
    try {
        Response.WriteJSONObject(new { Result = "See hello" });
    } 
    catch (Exception err) {
        if (err.Message != "Thread was being aborted.")
            Response.WriteJSONObject(new { Result = err.Message });
        else {
            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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x