繁体   English   中英

Response.End()不会中止当前线程

[英]Response.End () doesn't abort current thread

有人知道为什么ASP.NET可能不会使用Response.End()中止当前线程吗?

更新:原因是尽管Response.End()之后有代码(尽管编写得不好)被执行。 我从未见过Response.End()没有阻止当前线程执行的情况。


protected void Page_Load(object sender, EventArgs e)
{
        Response.Clear();
        Response.Redirect("somewhere", true);
        Response.End();

        //Some other code get's executed here
}

如您所指出的,方法Response.End定义为:

public void End()
{
  if (this._context.IsInCancellablePeriod) {
    InternalSecurityPermissions.ControlThread.Assert();
    Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
  }
  else if (!this._flushing)
  {
    this.Flush();
    this._ended = true;
    if (this._context.ApplicationInstance != null) {
      this._context.ApplicationInstance.CompleteRequest();
    }
  }
}

在Page_Load方法中使用断点调试相当简单的Web应用程序,我可以看到调用堆栈包括以下行:

System.Web.dll! System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.CallHandlerExecutionStep},引用布尔值已同步完成= true)+ 0x4c字节

反映到CallHandlerExecutionStep我可以看到属性IsCancellable定义为:

bool HttpApplication.IExecutionStep.IsCancellable {
  get {
    return !(this._application.Context.Handler is IHttpAsyncHandler);
  }
}

.aspx页面的默认处理程序是实现IHttpHandler而不是PageHandlerFactory的输出-这将导致IsCancellable返回true(确实在我的测试应用程序中如此)。

您是否在根web.config或在堆栈的更上层配置了其他HttpHandler来使用异步处理程序? 例如,您是否在使用带有部分回发的更新面板?

您没有尝试块吗? 那将抛出ThreadAbortException。

根据http://msdn.microsoft.com/zh-CN/library/a8wa7sdt (VS.80) .aspx (选择.NET Framework 2.0)上的社区评论,当前线程无法在Global.asax中取消:

“小心!如果您传递的是true,则Response.Redirect()会调用Response.End(),但是在某些情况下,Response.End()不会引发ThreadAbortException,而是设置几个标志并返回。是当您使用Global.asax时,但在无法取消的任何执行步骤中都会更普遍地发生。我将在Response.End()中添加详细信息,但在页面中您会没事的,但是在Global中即使您将true作为第二个参数传递,.asax处理也将继续。”

我实际上在我的一个项目中注意到了这种行为。

我可能会误会,但是我相信,使用的一件事是属于线程池并被回收的后台线程,因此不会杀死线程。

Response.End(),终止Response,但不会从函数返回。

protected void Page_Load(object sender, EventArgs e) 
{ 
        If(sometingBad)
        {
        Response.Clear(); 
        Response.Redirect("somewhere", true); 
        Response.End(); 
        return;
        }

        //Some other code get's executed here 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM