How to Avoid Response.End() "Thread Was Being Aborted" Exception During the Excel File Download

Error: Excepiton in System.Threading.ThreadAbortException: Thread was being aborted

As answered here :- How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download

Replace this : HttpContext.Current.Response.End();

With this :

HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline**

chain of execution and directly execute the EndRequest event.

And answered here :- ASP.NET exception "Thread was being aborted" causes method to exit

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.

ThreadAbortException handling in asp.net while executing Response.End() method

Instead of

Response.End()

use

HttpContext.Current.ApplicationInstance.CompleteRequest()

Like this

protected void btn_click(object sender, EventArgs e)
{
try
{
string fileToDownload = MapPath(@"~\Sample.txt");
string fileToRead = MapPath(@"~\FileNotExist.txt");

try
{
//Section 1
try
{
// try to read the file which does not exist to raise the exception
StreamReader ss = new StreamReader(fileToRead);
}
catch (IOException IoEx)
{
// Just for sample exception
}

// Section 2 code block still execute because exception handled by upper try catch block
//Section 2

Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Length", fileToDownload.Length.ToString());
Response.AddHeader("Content-Disposition","attachment;filename=SampleTemplate.txt");
Response.ContentType = "text";
Response.WriteFile(fileToDownload);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

}
catch (System.Threading.ThreadAbortException abrtEx)
{

}

//Section 3 Code block not executing even after exception handeled by ThreadAbortException
//Section 3
string test = "Do futher process after sample downloaded";

}
catch (Exception ex) // Outer Catch Block
{
throw ex;
}
}

Thread was being aborted issue while trying to download pdf file

Response.End() in this case is used to prevent anything else from being added to the response as it may corrupt your file. The Server.Transfer(), Response.End(), Response.Redirect() all raise exceptions. Each of these methods internally call Response.End() and causes a ThreadAbortException exception.

Exception: Thread was being aborted when not dealing with a Response.End issue

So this is a bug I've been looking to sort out for over a year and all the other related questions seemed to deal with the Response.End() problem but here it wasn't the case.

I've always thought that there was something wrong in the methods where the bug occurred but actually it's where the worker gets called that the bug starts.

The solution is simple: I added .Wait() and since then the bug's gone from the logs.

System.Threading.Tasks.Task.Factory         
.StartNew(() => {
new MyApplicationWorker().RunApplicationWorker().Wait();
});

Thanks to alex.b for the comment that lead to this other question and I hope this helps others who'll bump into the same issue of worker threads being aborted.



Related Topics



Leave a reply



Submit