Click here to Skip to main content
13,200,729 members (53,591 online)
Rate this:
 
Please Sign up or sign in to vote.
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 12-Oct-17 8:32am
Updated 12-Oct-17 9:17am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
s23gaurav 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171020.1 | Last Updated 12 Oct 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100