Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Exception Message : Thread was being aborted.

I am getting above exception when running the application in prod server. I never get that exception in dev and in test environment. 
Could any please help me out. If there is anything wrong in the code. 

My code in vb.


  Try
                    Dim filePath As String = Request.QueryString("fn")
                    Response.ContentType = ContentType
                    Response.AppendHeader("content-disposition", "attachment; filename=" + Path.GetFileName(filePath))
                    Response.TransmitFile(filePath)
                    Response.Flush()

                    System.Threading.Thread.Sleep(1000)
                    If File.Exists(filePath) Then
                        File.Delete(filePath)
                    End If

                    Response.End()
                Catch ex As Exception
                    oUtility.WriteLog("Exception Message : " + ex.Message, True)
                    vm.stat = 2
                    vm.message = "Internal server error"
                    Throw
                End Try



My code in C#

  string filePath = Request.QueryString["fn"];
                        Response.ContentType = ContentType;
                        Response.AppendHeader("content-disposition", "attachment; filename=" + Path.GetFileName(filePath));
                        Response.TransmitFile(filePath);
                        Response.Flush();

                        System.Threading.Thread.Sleep(1000);
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                        //response.writefile(filepath)
                        Response.End();


What I have tried:

I tried to run the application in dev environment. I never got that issue. I could not reproduce the error.
Posted
Updated 12-Jun-21 23:39pm

This is by design:
To mimic the behavior of the End method in ASP, this method tries to raise a ThreadAbortException exception. If this attempt is successful, the calling thread will be aborted, which is detrimental to your site's performance. In that case, no code after the call to the End method is executed.

Remove the Response.End() call, and replace it with Application.CompleteRequest(), as suggested in the documentation.

HttpApplication.CompleteRequest Method (System.Web) | Microsoft Docs[^]
 
Share this answer
 
Comments
s23user 12-Oct-17 21:06pm    
Sir, real issue is, as you could see above code is to download the file passed in query string. we have two server in prod. and load balancer. in prod while we call the code to download the file, It does not get downloaded and in log file i get that message as exception. So could you tell me what could have gone wrong? looking at the code. In dev environment i never had this issue.
Richard Deeming 13-Oct-17 7:50am    
Read my answer again. If you call Response.End, you will get a ThreadAbortException.
I used Response.End to avoid the "Word found unreadable content" error after downloading (caused apparently by Word finding some unexpected data at the end of the file). This works fine in Dev but causes a thread error in Prod, exactly as this article states. Replacing this with application.completerequest as advised here re-introduces this Word error in Dev but not in Prod ie download works ok and no thread error in Prod so I just ignore the error in Dev.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900