关于Response.redirect和Response.End出现线程中止异常的处理
2013-10-20 10:43
579 查看
最近做了一个项目其中使用了多线程获取POST过来的数据后再Response回复,但由于是多线程,在Response.End()的时候报出了异常:
后来在微软的Support里面查到相关资料,不过微软在给出问题出错原因时显得很搞笑:这种现象是设计使然。以下是官方解释和解决方案。
症状
如果使用 Response.End、Response.Redirect 或 Server.Transfer 方法,则出现 ThreadAbortException 异常。 可使用 try-catch 语句捕捉此异常。
原因
Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件。 Response.End 后面的代码行将不执行。此问题出现在 Response.Redirect 和 Server.Transfer 方法中,这是由于这两种方法都在内部调用 Response.End。
解决方案
若要解决此问题,请使用下列方法之一:
对于 Response.End,调用 ApplicationInstance.CompleteRequest 方法而不调用 Response.End,以便跳过 Application_EndRequest 事件的代码执行。
对于 Response.Redirect,使用重载 Response.Redirect(String url, bool endResponse),对 endResponse 参数它传递 false以取消对 Response.End 的内部调用。例如:
如果使用这种解决方法,Response.Redirect 后面的代码将得到执行。
对于 Server.Transfer,请改用 Server.Execute 方法。
2013-10-20 10:05:31,606 responseText - at System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End()
后来在微软的Support里面查到相关资料,不过微软在给出问题出错原因时显得很搞笑:这种现象是设计使然。以下是官方解释和解决方案。
症状
如果使用 Response.End、Response.Redirect 或 Server.Transfer 方法,则出现 ThreadAbortException 异常。 可使用 try-catch 语句捕捉此异常。
原因
Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件。 Response.End 后面的代码行将不执行。此问题出现在 Response.Redirect 和 Server.Transfer 方法中,这是由于这两种方法都在内部调用 Response.End。
解决方案
若要解决此问题,请使用下列方法之一:
对于 Response.End,调用 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 后面的代码将得到执行。
对于 Server.Transfer,请改用 Server.Execute 方法。
相关文章推荐
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理(转)
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于Response.redirect和Response.End出现线程中止异常的处理
- 关于response.redirect和request.end 出现线程终止异常的处理
- Response.End ,Response.Redirect、Server.Transfer 引发 “正在中止线程”异常的问题
- Response.Redirect 与 异常(线程正在中止)
- Response.End导致“正在中止线程”异常的问题
- 在try catch中使用Response.End()抛"线程被中止"异常
- 如果使用 Response.End、Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常
- C# 指定物理目录下载文件,Response.End导致“正在中止线程”异常的问题
- C# try...catch... 里的 Response.End() 引发线程中止异常