简体   繁体   English

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

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

Anybody know why ASP.NET might not abort the current thread with a Response.End()? 有人知道为什么ASP.NET可能不会使用Response.End()中止当前线程吗?

Update: Reason being that there is code, albeit not well written, that is getting executed after Response.End(). 更新:原因是尽管Response.End()之后有代码(尽管编写得不好)被执行。 I've never seen a case where Response.End () didn't stop the current thread from executing. 我从未见过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
}

As you've pointed out, the method Response.End is defined as: 如您所指出的,方法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();
    }
  }
}

Debugging a fairly simple web app with a break point in the Page_Load method, I can see the call stack includes the line: 在Page_Load方法中使用断点调试相当简单的Web应用程序,我可以看到调用堆栈包括以下行:

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

Reflecting into CallHandlerExecutionStep I can see that the property IsCancellable is defined as: 反映到CallHandlerExecutionStep我可以看到属性IsCancellable定义为:

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

The default handler for .aspx pages is the output from the PageHandlerFactory which implement IHttpHandler, not IHttpAsyncHandler - which would result in IsCancellable returning true (as indeed it does in my test app). .aspx页面的默认处理程序是实现IHttpHandler而不是PageHandlerFactory的输出-这将导致IsCancellable返回true(确实在我的测试应用程序中如此)。

Have you configured a different HttpHandler in either your root web.config or one further up the stack to use an Async Handler instead? 您是否在根web.config或在堆栈的更上层配置了其他HttpHandler来使用异步处理程序? Are you using Update Panels with Partial Postbacks for example? 例如,您是否在使用带有部分回发的更新面板?

You don't have it in a try block do you? 您没有尝试块吗? That would throw a ThreadAbortException. 那将抛出ThreadAbortException。

According to Community comment on http://msdn.microsoft.com/en-us/library/a8wa7sdt(VS.80).aspx (select .NET framework 2.0) the current thread is not cancellable in Global.asax: 根据http://msdn.microsoft.com/zh-CN/library/a8wa7sdt (VS.80) .aspx (选择.NET Framework 2.0)上的社区评论,当前线程无法在Global.asax中取消:

"Careful!! If you pass true, Response.Redirect() calls Response.End(), however there are certain circumstances where Response.End() does not raise the ThreadAbortException and instead sets a couple of flags and returns. One such situation is when you're in Global.asax, but it would occur more generically in any execution step that was not cancellable. I'll add details on Response.End(), but in a page you'd be fine, but in Global.asax processing will continue even if you pass true as the second parm." “小心!如果您传递的是true,则Response.Redirect()会调用Response.End(),但是在某些情况下,Response.End()不会引发ThreadAbortException,而是设置几个标志并返回。是当您使用Global.asax时,但在无法取消的任何执行步骤中都会更普遍地发生。我将在Response.End()中添加详细信息,但在页面中您会没事的,但是在Global中即使您将true作为第二个参数传递,.asax处理也将继续。”

I actually noticed this behaviour in one of my projects. 我实际上在我的一个项目中注意到了这种行为。

I could be mistaken, but I believe for one thing the threads that are used are background threads that belong to a thread pool and are recycled so it doesn't kill the thread. 我可能会误会,但是我相信,使用的一件事是属于线程池并被回收的后台线程,因此不会杀死线程。

Response.End(), terminates the Response, but it doesn't return from the function. 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