ThreadAbortException

This blog is wrote by Louis Yan, if you want to use this article, please comment where it from, thanks!

 

When I Executing my Excel exporting method, it throw an exception like this:

   

 

Exception Message

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. 

线程终止

 The original code in my project is :

 

 1  try
 2          {
 3              FileInfo file  =   new  FileInfo(path);
 4              Response.Clear();
 5              Response.AddHeader( " Content-Disposition " " attachment; filename= "   +  file.Name);
 6              Response.AddHeader( " Content-Length " , file.Length.ToString());
 7              Response.ContentType  =   " application/octet-stream " ;
 8              Response.WriteFile(file.FullName);
 9              Response.End();
10          }
11           catch  (Exception e)
12          {
13              Response.Write( " " color:red;font - size = 20px;font - weight:bold\ " >Invalid download session

" );
14               throw   new  Exception(e.Message);
15          }

 

 When receiving this exception,  I check my codes first, but unfortunate there are no problem in the Excel export method, so I google for the error message. The Officially explanation is like this:

Reference: http://support.microsoft.com/kb/312629/EN-US/ 

Symptoms: If you use the Response.End()Response.Redirect(), or Server.Transfer() method, aThreadAbortException exception occurs. You can use a try-catch statement to catch this exception.  

Cause: The Response.End method ends the page execution and shifts the execution to theApplication_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.

This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally. 

Resolution: To work around this problem, use one of the following methods:

  • For Response.End, call theHttpContext.Current.ApplicationInstance.CompleteRequest method instead ofResponse.End to bypass the code execution to the Application_EndRequest event.
  • For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End. For example:
1   Response.Redirect ( " nextpage.aspx " false );

 

 So I use the resolution supplied by Microsoft, but there has another problem happened. so in this way, the resolution is not fit for my scenario, and I try for some a way to resolve the problem, the final resolution is : move the Response.End() Method to the Finally() section.

 1           try
 2          {
 3              FileInfo file  =   new  FileInfo(path);
 4              Response.Clear();
 5              Response.AddHeader( " Content-Disposition " " attachment; filename= "   +  file.Name);
 6              Response.AddHeader( " Content-Length " , file.Length.ToString());
 7              Response.ContentType  =   " application/octet-stream " ;
 8              Response.WriteFile(file.FullName);
 9          }
10           catch  (Exception e)
11          {
12              Response.Write( " " color:red;font - size = 20px;font - weight:bold\ " >Invalid download session

" );
13               throw   new  Exception(e.Message);
14          }
15           finally
16          {
17              Response.End();
19              Response.Clear();
20          }

 

 

 

 

 

转载于:https://www.cnblogs.com/xiaochuncha/archive/2009/12/25/1632018.html

你可能感兴趣的:(ThreadAbortException)