如何处理线程被中止异常vb.net/C#

本文关键字:异常 vb net 何处理 处理 线程 | 更新日期: 2023-09-27 18:12:36

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

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

Was getting Server cannot append header after HTTP headers have been sent exception.

所以我把代码改成了
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中的这个异常也不是每次都发生(有时会成功处理)。

如何处理线程被中止异常vb.net/C#

最后,这帮助我处理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();
         }
   }

现在我松了一口气。

IIS是这样工作的。它为新请求启动一个新线程。线程做它的事情,当它结束时,线程被中止。通常,这发生在。net管道中,它的句柄在那里。但是,如果您执行了Server.Redirect()之类的操作,它也会在代码中被终止。你自己完成请求也是一样。IIS说"它已经发送了返回,所以杀死它。"这就是它的工作原理。

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