且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

异常消息:线程正在中止。

更新时间:2023-12-05 10:27:22

这是设计好的:

为了模仿ASP中End方法的行为,此方法尝试引发ThreadAbortException异常。如果此尝试成功,则调用线程将被中止,这对您站点的性能不利。在这种情况下,执行End方法调用后没有代码。



删除 Response.End()调用,并按照文档中的建议将其替换为 Application.CompleteRequest()



HttpApplication.CompleteRequest Method(System.Web)| Microsoft Docs [ ^ ]


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.

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[^]