在 System.Threading.ThreadAbortException 中第一次偶然出现的“mscorlib.dll”类型的异常
“System.Threading.ThreadAbortException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理
但不影响程序的正常运行。于是在网上查了查,发现相关资料不多。后来找到微软的官方解释,搞定。
--------------------------------------------------------------------------------------------------------------

症状

如果使用 Response.EndResponse.Redirect Server.Transfer 方法,则出现 ThreadAbortException 异常。 可使用 try-catch 语句捕捉此异常。

原因

Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件。 Response.End 后面的代码行将不执行。
此问题出现在 Response.Redirect Server.Transfer 方法中,这是由于这两种方法都在内部调用 Response.End

解决方案

若要解决此问题,请使用下列方法之一:

  • 对于 Response.End,调用 ApplicationInstance.CompleteRequest 方法而不调用 Response.End,以便跳过 Application_EndRequest事件的代码执行。
  • 对于 Response.Redirect,使用重载 Response.Redirect(String url, bool endResponse),对 endResponse 参数它传递 false以取消对 Response.End 的内部调用。例如:
      Response.Redirect ("nextpage.aspx", false);

    如果使用这种解决方法,Response.Redirect 后面的代码将得到执行。

  • 对于 Server.Transfer,请改用 Server.Execute 方法。

状态

这种现象是设计使然。

又有问题出现了:

在try catch中使用Response.End()抛"线程被中止"异常,Response.Redirect()和Server.Transfer()也会出现这个问题.

如:(

try

        {

if (DoSomeThing())

            {

                Response.End();

//HttpContext.Current.ApplicationInstance.CompleteRequest();

            }

//DoOtherThing不写在else里只是为了说明问题

            DoOtherThing();

        }

catch (System.Threading.ThreadAbortException ex)

        {

            WirteLog(ex);

        }

catch (Exception ex)

        {

            WirteLog(ex);

        }

)

如果不用catch (System.Threading.ThreadAbortException ex),就会抛"线程被中止"异常,

如果不用catch (System.Threading.ThreadAbortException ex),而用HttpContext.Current.ApplicationInstance.CompleteRequest 代替Response.End(),则后面的DoOtherThing()还是会继续执行.

要根据实际需要选择具体做法.

最终解决办法:

(1)、在页面的OnLoad或OnInit中可以使用HttpContext.Current.ApplicationInstance.CompleteRequest()+return来代替Response.End;

(2)、用 Response.Redirect(strUrl,false)+return 来代替 Response.Redirect(strUrl)

(3)、Server.Execute根本代替不了Server.Transfer方法,Server.Execute会将两个页面的代码全部输出。

特别return注意:return语句退出本语句所在的方法,而不能退出子类的方法!千万不要以为在基类页面的OnLoad中检测到了某object==null时就能return此次请求,子类页面的OnLoad就无需检测此object!=null可照用!

做这个网站需要下载软件 可我有不想让 迅雷等工具 下载影响网速.

就做了个限制程序!

可就出现了错误 !   正在中止线程

程序大致如下:

  1. try
  2. {  
  3. sting host = context.Request.UrlReferrer.Host;  
  4. if ( 程序判断)  
  5. {     
  6. context.Response.Clear();  
  7. context.Response.ContentType = "application/octet-stream";  
  8. context.Response.WriteFile(file.FullName);  
  9. context.Response.End();  
  10. }  
  11. }  
  12. catch (Exception e)  
  13. {  
  14. HttpContext.Current.Response.Redirect("/default.aspx?key=" + e.Message);  
  15. context.Response.End();  
  1. try
  2. sting host = context.Request.UrlReferrer.Host; 
  3. if ( 程序判断) 
  4. {    
  5. context.Response.Clear(); 
  6. context.Response.ContentType = "application/octet-stream"; 
  7. context.Response.WriteFile(file.FullName); 
  8. context.Response.End(); 
  9. catch (Exception e) 
  10. HttpContext.Current.Response.Redirect("/default.aspx?key=" + e.Message); 
  11. context.Response.End(); 

只有 一执行 Try 里面的东西 就一定出错 e.Message=正在中止线程

研究了一下好像是 Try 执行的时候 context.Response.End(); 就出错

不能把context.Response.End();放在 Try 里面

需要写成:

  1. try
  2. {  
  3. sting host = context.Request.UrlReferrer.Host;  
  4. }  
  5. catch (Exception e)  
  6. {  
  7. HttpContext.Current.Response.Redirect("/default.aspx?key=" + e.Message);  
  8. context.Response.End();  
  9. }  
  10. if ( 程序判断)  
  11. {     
  12. context.Response.Clear();  
  13. context.Response.ContentType = "application/octet-stream";  
  14. context.Response.WriteFile(file.FullName);  
  15. context.Response.End();  
  16. }

System.Threading.ThreadAbortException: 正在中止线程。的更多相关文章

  1. C#错误之 System.Threading.ThreadAbortException:正在中止线程

    参考:http://www.cnblogs.com/chendaoyin/archive/2013/06/27/3159211.html 1.开启一个子线程 //开启一个子线程,子线程调用方法 Met ...

  2. C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程

    C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程 这是由ThreadAbortException抛出的 可以写成下面的样子 tr ...

  3. 【异常记录(九)】 System.Threading.ThreadAbortException: 正在中止线程

    报错如下: System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.Ab ...

  4. System.Threading.ThreadAbortException: 正在中止线程

    症状 如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常.您可以使用 try-ca ...

  5. “System.Threading.ThreadAbortException”类型的第一次机会异常在 mscorlib.dll 中发

    问题原因: Thread.Abort 方法 .NET Framework 4  其他版本   1(共 1)对本文的评价是有帮助 - 评价此主题 在调用此方法的线程上引发 ThreadAbortExce ...

  6. 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。System.Threading.ThreadAbortException

    第一次遇到这样的错误 错误语法 try{ Response.Redirect("aa.aspx"); }catch (Exception ex){ Response.Redirec ...

  7. ASP.NET: 正在中止线程 错误原及解决方法

    #[操作记录]:2010-02-23 9:25:12  System.Threading.ThreadAbortException: 正在中止线程. 症状 如果使用 Response.End.Resp ...

  8. 【转】ASP.NET"正在中止线程"错误原因

    最近做的系统中老出现的一些问题不太明白,在使用 Response.End.Response.Redirect 或 Server.Transfer 时出现 ThreadAbortException , ...

  9. 异常System.Threading.Thread.AbortInternal

    异常信息: System.Threading.ThreadAbortException: 正在中止线程. 在 System.Threading.Thread.AbortInternal() 在 Sys ...

随机推荐

  1. C++的三种继承方式简述

    C++对父类(也称基类)的继承有三种方式,分别为:public继承.protected继承.private继承.三种继承方式的不同在于继承之后子类的成员函数的"可继承性质". 在说 ...

  2. 2016 - 1- 22 NSURLConnetction --- POST请求

    一:与上一篇博客中的GET方法类似  只不过需要多注意,如果要改变请求的类型,需要生成NSMutableURLRequest对象才可以设置请求的类型. NSURL *url = [NSURL URLW ...

  3. return, exit, _exit的区别

    return是返回的最常用的方式 _exit属于POSIX定义的系统调用 exit是GLIBC封装之后的函数 1 _exit和exit都会导致整个进程退出,清理进程所占用的资源,但是glibc封装ex ...

  4. Java中接口作为方法的返回

    在<算法>中的散列表一节,在用拉链法实现散列表的API时要求实现以下一个方法: public Iterable<Key> keys() 我们知道Iterable是一个接口,那么 ...

  5. Emacs+highlight-parentheses高亮括号

    EmacsWiki上关于它的介绍HighlightParentheses,下载最新版请通过作者的GitHub:https://github.com/nschum/highlight-parenthes ...

  6. LeetCode----Unique Binary Search Trees 2

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. HTML中的边框属性

    可以通过边框风格属性border-style设定上下左右边框的风格,该属性用于设置一个元素边框的样式,而且必须用于指定可见的边框.可以使用1到4个关键字,如果四个值都给出了,它们分别用于上.右.下和左 ...

  8. All X_数的快速幂

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  9. HDU 4054

    http://acm.hdu.edu.cn/showproblem.php?pid=4054 模拟题,对一个字符串的每个字符输出16进制表示的数字,每行处理16个字符,后面再把这16个字符输出,大小写 ...

  10. 怎样将某一类型标识为适合绑定到 System.Web.UI.WebControls.ObjectDataSource 对象的对象

    1.页面的代码如下: body> <form id="form1" runat="server"> <div> </div& ...