C# ASP.NET 异常“线程被中止”导致方法退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1014439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET exception "Thread was being aborted" causes method to exit
提问by Ryan Sampson
In the code below, sometimes someFunctionCall()
generates an exception:
在下面的代码中,有时会someFunctionCall()
产生异常:
Thread was being aborted.
威胁已经被清除了。
How come the code in code Block B never runs? Does ASP.NET start a new thread for each method call? I was suprised to see that when this exception happens the code in Block B never runs, the method returns, and my application keeps running. Can someone please explain this?
为什么代码块 B 中的代码永远不会运行?ASP.NET 是否为每个方法调用启动一个新线程?我很惊讶地看到,当这个异常发生时,Block B 中的代码永远不会运行,方法返回,我的应用程序继续运行。有人可以解释一下吗?
public void method()
{
// CODE BLOCK A
//...
try
{
someFunctionCall(); // this call is generating thread abort exception
}
catch(Exception ex)
{
// log exception message
}
// CODE BLOCK B
// ...
}
采纳答案by SLaks
This is a ThreadAbortException
; it's a special exception that is automatically rethrown at the end of every catch block, unless you call Thread.ResetAbort()
.
这是一个ThreadAbortException
; 这是一个特殊的异常,它会在每个 catch 块的末尾自动重新抛出,除非您调用Thread.ResetAbort()
.
ASP .Net methods like Response.End
or Response.Redirect
(unless you pass false
) throw this exception to end processing of the current page; your someFunctionCall()
is probably calling one of those methods.
ASP .Net 方法像Response.End
or Response.Redirect
(除非你通过false
)抛出这个异常来结束当前页面的处理;您someFunctionCall()
可能正在调用其中一种方法。
ASP .Net itself handles this exception and calls ResetAbort
to continue processing.
ASP .Net 自己处理这个异常并调用ResetAbort
继续处理。
回答by Tarik
Try this way :
试试这个方法:
This is my extension method :
这是我的扩展方法:
public static void WriteJSONObject(this HttpResponse response, object content) {
response.ContentType = "application/json";
response.Write(new JavaScriptSerializer().Serialize(content));
response.End();
}
And the logic :
和逻辑:
public void RegisterUser() {
try {
Response.WriteJSONObject(new { Result = "See hello" });
}
catch (Exception err) {
if (err.Message != "Thread was being aborted.")
Response.WriteJSONObject(new { Result = err.Message });
else {
Response.End();
}
}
}
回答by user3095353
To work around this problem, use one of the following methods:
For Response.End
, call the HttpContext.Current.ApplicationInstance.CompleteRequest
method instead of Response.End
to bypass the code execution to the Application_EndRequest
event.
要解决此问题,请使用以下方法之一: 对于Response.End
,调用HttpContext.Current.ApplicationInstance.CompleteRequest
方法而不是Response.End
绕过Application_EndRequest
事件的代码执行。
For Response.Redirect
, use an overload, Response.Redirect(String url, bool endResponse)
that passes false for the endResponse
parameter to suppress the internal call to Response.End
. For example:
对于Response.Redirect
,使用重载,Response.Redirect(String url, bool endResponse)
为endResponse
参数传递 false以抑制对 的内部调用Response.End
。例如:
Response.Redirect ("nextpage.aspx", false);
If you use this workaround, the code that follows Response.Redirect
is executed.
For Server.Transfer
, use the Server.Execute
method instead.
如果您使用此解决方法,Response.Redirect
则会执行后面的代码。对于Server.Transfer
,请改用Server.Execute
方法。