zoukankan      html  css  js  c++  java
  • ASP.NET: 正在中止线程 错误原及解决方法

    症状


    如果使用 Response.EndResponse.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException异常。您可以使用 try-catch 语句捕获此异常。主要原因是Response.EndResponse.Redirect 或 Server.Transfer 方法会用Thread.Abort方法立刻中止IIS的工作线程,引发ThreadAbortException异常。
     
     

    原因


    Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件。不执行 Response.End 后面的代码行。

    此问题出现在 Response.Redirect 和 Server.Transfer 方法中,因为这两种方法均在内部调用Response.End
     
     

    解决方案


    要解决此问题,请使用下列方法之一:
    • 对于 Response.End,调用 HttpContext.Current.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 后面的代码。如有必要可以在 Response.Redirect 后加上return语句,立刻返回当前函数,防止执行多余的代码。

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

    此外我们可以借鉴这篇文章的内容来扩展Response.Redirec方法:

    高效的使用Response.Redirect解决一些不必要的问题

    原文链接

  • 相关阅读:
    msyql多个or,and,
    mysql中 where in 用法详解
    history.back(-1)和history.go(-1)的区别
    经典 mysql 28道题
    企业案例(二):增量恢复案例
    企业案例(一):由于mysql sleep线程过多小故障
    mysql数据库恢复
    binlog介绍
    mysql 数据库备份
    docker入门与实践
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/9072488.html
Copyright © 2011-2022 走看看