[英]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.