Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Response.End()不';中止当前线程_C#_Asp.net - Fatal编程技术网

C# Response.End()不';中止当前线程

C# Response.End()不';中止当前线程,c#,asp.net,C#,Asp.net,有人知道为什么ASP.NET不能用响应中止当前线程。End() 更新:原因是在Response.End()之后执行了一些代码,尽管编写得不好。我从未见过Response.End()不停止当前线程执行的情况 protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.Redirect("somewhere", true); Response.En

有人知道为什么ASP.NET不能用响应中止当前线程。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()终止响应,但它不会从函数返回

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 
} 

我可能弄错了,但我相信一件事,所使用的线程是属于线程池的后台线程,可以循环使用,这样就不会杀死线程

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

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 
} 

正如您所指出的,方法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();
    }
  }
}
bool HttpApplication.IExecutionStep.IsCancellable {
  get {
    return !(this._application.Context.Handler is IHttpAsyncHandler);
  }
}
在Page_Load方法中调试一个带有断点的相当简单的web应用程序时,我可以看到调用堆栈包含以下行:

System.Web.dll!System.Web.HttpApplication.ExecuteTep(System.Web.HttpApplication.IExecutionStep={System.Web.HttpApplication.CallHandlerExecutionStep},ref bool completedSynchronously=true)+0x4c字节

CallHandlerExecutionStep
中,我可以看到属性
IsCancellable
被定义为:

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();
    }
  }
}
bool HttpApplication.IExecutionStep.IsCancellable {
  get {
    return !(this._application.Context.Handler is IHttpAsyncHandler);
  }
}
.aspx页面的默认处理程序是
PageHandlerFactory
的输出,它实现了IHttpHandler,而不是IHttpAsyncHandler-这将导致
IsCancellable
返回true(在我的测试应用程序中确实如此)


您是否在根web.config或更高的堆栈中配置了不同的HttpHandler以使用异步处理程序?例如,您是否使用带有部分回发的更新面板?

正如您所指出的,方法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();
    }
  }
}
bool HttpApplication.IExecutionStep.IsCancellable {
  get {
    return !(this._application.Context.Handler is IHttpAsyncHandler);
  }
}
在Page_Load方法中调试一个带有断点的相当简单的web应用程序时,我可以看到调用堆栈包含以下行:

System.Web.dll!System.Web.HttpApplication.ExecuteTep(System.Web.HttpApplication.IExecutionStep={System.Web.HttpApplication.CallHandlerExecutionStep},ref bool completedSynchronously=true)+0x4c字节

CallHandlerExecutionStep
中,我可以看到属性
IsCancellable
被定义为:

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();
    }
  }
}
bool HttpApplication.IExecutionStep.IsCancellable {
  get {
    return !(this._application.Context.Handler is IHttpAsyncHandler);
  }
}
.aspx页面的默认处理程序是
PageHandlerFactory
的输出,它实现了IHttpHandler,而不是IHttpAsyncHandler-这将导致
IsCancellable
返回true(在我的测试应用程序中确实如此)


您是否在根web.config或更高的堆栈中配置了不同的HttpHandler以使用异步处理程序?例如,您是否正在使用带有部分回发的更新面板?

您在try块中没有它,是吗?这会引发线程异常

你没有试过,是吗?这会引发线程异常

根据社区评论(选择.NET framework 2.0),当前线程在Global.asax中不可取消:

“小心!!如果传递true,Response.Redirect()将调用Response.End(),但是在某些情况下Response.End()会不会引发ThreadAbortException,而是设置两个标志和返回。其中一种情况是当您处于Global.asax中时,但在任何不可取消的执行步骤中都会更普遍地发生。我将添加有关Response.End()的详细信息,但在一个页面中可以,但在Global.asax中,即使您将true作为第二个parm传递,处理仍将继续。”


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

根据社区评论(选择.NET framework 2.0),当前线程在Global.asax中不可取消:

“小心!!如果传递true,Response.Redirect()将调用Response.End(),但是在某些情况下Response.End()会不会引发ThreadAbortException,而是设置两个标志和返回。其中一种情况是当您处于Global.asax中时,但在任何不可取消的执行步骤中都会更普遍地发生。我将添加有关Response.End()的详细信息,但在一个页面中可以,但在Global.asax中,即使您将true作为第二个parm传递,处理仍将继续。”


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

为什么它会中止线程?当然,该线程将返回到线程池中。(注意:自1999年以来,我就没有研究过ASP的线程模型;自上次查看该代码以来,情况可能发生了变化。)Response.End()会中止线程@Cheeso,应该是这样的,但我的情况是有东西在阻止。Rick Strahl有一个小代码段,显示了一个不会中止当前线程的潜在案例@特伦特,但即使是他也不知道它为什么会起作用,这有点微妙的缺陷——看看它应该如何工作,异常应该在他捕获后继续出现,并且仍然不会记录他的横幅调用。@Zhaph,我真的是指他概述的MS在响应时所做的代码。end()::public void end(){if(this._context.isincancellablepiriod){InternalSecurityPermissions.ControlThread.Assert();Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));}否则如果(!this._Flush){this.Flush();this._end=true;如果(this._context.ApplicationInstance!=null){this._context.ApplicationInstance.CompleteRequest();}}}}为什么它会中止线程?当然,该线程将返回到线程池中。(注意:自1999年以来,我没有处理过ASP的线程模型;自上次查看该代码以来,情况可能发生了变化。)Response.End()确实中止了线程。@Cheeso,应该是这样,但我的情况是,有东西在前面