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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:34:26  来源:igfitidea点击:

ASP.NET exception "Thread was being aborted" causes method to exit

c#.netasp.net

提问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.Endor 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.Endor Response.Redirect(除非你通过false)抛出这个异常来结束当前页面的处理;您someFunctionCall()可能正在调用其中一种方法。

ASP .Net itself handles this exception and calls ResetAbortto 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.CompleteRequestmethod instead of Response.Endto bypass the code execution to the Application_EndRequestevent.

要解决此问题,请使用以下方法之一: 对于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 endResponseparameter 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.Redirectis executed. For Server.Transfer, use the Server.Executemethod instead.

如果您使用此解决方法,Response.Redirect则会执行后面的代码。对于Server.Transfer,请改用Server.Execute方法。