Search code examples
c#vb.netexceptionhttpresponsethreadabortexception

How to handle Thread was being aborted Exception vb.net/C#?


I've seen several questions regarding this issue but I've not found an apt answer for my issue. Initially I was using the following code after writing json in a function

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();

Was getting Server cannot append header after HTTP headers have been sent exception.

So I altered the code to

try {
    HttpContext.Current.Response.Write(Data);
    HttpContext.Current.Response.End();
} catch (System.Threading.ThreadAbortException exc) {
    try {
        //Sends the response buffer
        HttpContext.Current.Response.Flush();
        // Prevents any other content from being sent to the browser
        HttpContext.Current.Response.SuppressContent = true;
        //Directs the thread to finish, bypassing additional processing
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    } catch (Exception ex) {
              //Log Exception
    }

}

All these code are in function(lets say) writeData() and its called by a function called CallWriteData. Now the exception has been successfully handled in WriteData() function but its throwing Thread was being aborted exception in the parent function CallWriteData.

To be honest its not a major issue in my project but it would be nice if I fix this annoying issue. Also this exception in CallWriteData not every-time(sometimes its successfully handled).


Solution

  • Finally,this helped me to handle Thread was being aborted exception,

    try
    {
       //Write HTTP output
        HttpContext.Current.Response.Write(Data);
    }  
    catch (Exception exc) {}
    finally {
       try 
        {
          //stop processing the script and return the current result
          HttpContext.Current.Response.End();
         } 
       catch (Exception ex) {} 
       finally {
            //Sends the response buffer
            HttpContext.Current.Response.Flush();
            // Prevents any other content from being sent to the browser
            HttpContext.Current.Response.SuppressContent = true;
            //Directs the thread to finish, bypassing additional processing
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //Suspends the current thread
            Thread.Sleep(1);
         }
       }
    

    if you use the following the following code instead of HttpContext.Current.Response.End() , you will get Server cannot append header after HTTP headers have been sent exception.

                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.SuppressContent = True;
                HttpContext.Current.ApplicationInstance.CompleteRequest();
    

    Another Fix which I found is Thread.BeginCriticalRegion();

       try 
     {
        //Write HTTP output
       HttpContext.Current.Response.Write(Data);
      } catch (Exception exc) {} 
      finally {
        try {
         //Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.
         Thread.BeginCriticalRegion();
         HttpContext.Current.Response.End();
             } catch (Exception ex) {} 
        finally {
        //Sends the response buffer
        HttpContext.Current.Response.Flush();
        // Prevents any other content from being sent to the browser
        HttpContext.Current.Response.SuppressContent = true;
        //Directs the thread to finish, bypassing additional processing
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        Thread.EndCriticalRegion();
             }
       }
    

    Now I'm a relieved man.