如何处理Thread was being aborted Exception vb.net/C#?

How to handle Thread was being aborted Exception vb.net/C#?

我已经看到几个关于这个问题的问题,但我没有找到适合我的问题的答案。 最初我在函数

中编写 json 后使用以下代码
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();

出现 Server cannot append header after HTTP headers have been sent 异常。

所以我将代码更改为

try {
    HttpContext.Current.Response.Write(Data);
    HttpContext.Current.Response.End();
} catch (System.Threading.ThreadAbortException exc) {
    try {
        //Sends the response buffer
        HttpContext.Current.Response.Flush();
        // Prevents any other content from being sent to the browser
        HttpContext.Current.Response.SuppressContent = true;
        //Directs the thread to finish, bypassing additional processing
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    } catch (Exception ex) {
              //Log Exception
    }

}

所有这些代码都在函数中(比方说)writeData() 并由一个名为 CallWriteData 的函数调用。现在异常已在 WriteData() 函数中成功处理,但它在父函数 CallWriteData.

中抛出 Thread was being aborted 异常

老实说,这不是我项目中的主要问题,但如果我能解决这个烦人的问题就好了。此外,CallWriteData 中的此异常并非每次都发生(有时会成功处理)。

这就是 IIS 的工作原理。它为新请求启动一个新线程。线程做它的事情,当它结束时线程被中止。通常,这发生在 .NET 管道中,它是那里的句柄。但是,如果您执行 Server.Redirect() 之类的操作,它也会在您的代码中中止。与您自己完成请求类似。 IIS 说 "It's sent the return, so kill it." 这就是它的工作原理。

(线程可能被保存以供另一个请求重用,但刚刚完成的代码被中止。)

最后,这帮助我处理了 Thread was being aborted 异常,

try
{
   //Write HTTP output
    HttpContext.Current.Response.Write(Data);
}  
catch (Exception exc) {}
finally {
   try 
    {
      //stop processing the script and return the current result
      HttpContext.Current.Response.End();
     } 
   catch (Exception ex) {} 
   finally {
        //Sends the response buffer
        HttpContext.Current.Response.Flush();
        // Prevents any other content from being sent to the browser
        HttpContext.Current.Response.SuppressContent = true;
        //Directs the thread to finish, bypassing additional processing
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        //Suspends the current thread
        Thread.Sleep(1);
     }
   }

如果您使用以下代码而不是 HttpContext.Current.Response.End() ,您将得到 Server cannot append header after HTTP headers have been sent 异常。

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();

我发现的另一个修复是 Thread.BeginCriticalRegion();

   try 
 {
    //Write HTTP output
   HttpContext.Current.Response.Write(Data);
  } catch (Exception exc) {} 
  finally {
    try {
     //Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.
     Thread.BeginCriticalRegion();
     HttpContext.Current.Response.End();
         } catch (Exception ex) {} 
    finally {
    //Sends the response buffer
    HttpContext.Current.Response.Flush();
    // Prevents any other content from being sent to the browser
    HttpContext.Current.Response.SuppressContent = true;
    //Directs the thread to finish, bypassing additional processing
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    Thread.EndCriticalRegion();
         }
   }

现在我释然了。