Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理被中止的线程异常vb.net/C#?_C#_Vb.net_Exception_Httpresponse_Threadabortexception - Fatal编程技术网

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

如何处理被中止的线程异常vb.net/C#?,c#,vb.net,exception,httpresponse,threadabortexception,C#,Vb.net,Exception,Httpresponse,Threadabortexception,我已经看到了一些关于这个问题的问题,但我还没有找到一个适合我的问题的答案。 最初,我在函数中编写json后使用以下代码 HttpContext.Current.Response.Flush(); HttpContext.Current.Response.SuppressContent = True; HttpContext.Current.ApplicationInstance.CompleteRequest(); 正在获

我已经看到了一些关于这个问题的问题,但我还没有找到一个适合我的问题的答案。 最初,我在函数中编写json后使用以下代码

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
正在获取
服务器无法在发送HTTP标头后追加标头
异常

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
所以我把代码改成了

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
中中止
异常


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

IIS就是这样工作的。它为新请求启动一个新线程。线程执行它的任务,当它结束时,线程被中止。通常,这种情况发生在.NET管道系统中,并且在那里有手柄。但是,如果您执行类似于Server.Redirect()的操作,它也会被中止—在您的代码中。与此类似,您也可以自己完成请求。IIS说“它发送了返回,所以杀了它。”这就是它的工作原理


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

最后,这帮助我处理
线程被中止的异常

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()
,则会出现
服务器无法在发送HTTP头后追加头的异常

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = True;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
我发现的另一个修复是
Thread.BeginCriticalRegion()


现在我松了一口气。

好吧,我假设被中止的线程是http线程。如果在对writeData()的调用中中止了该线程,那么您为什么希望以后在同一线程上执行任何操作?如果writeData()已经处理了异常,为什么还要再次处理它?你的意思是为什么我在
CallWriteData
中处理它?是的。如果您从CallWriteData()调用writeData()来处理数据写入,并且它会处理所有相关的异常,那么为什么您要在CallWriteData()中重新引发该异常并再次处理它呢?在我的项目中,
writeData()
函数是从许多函数调用的,
CallWriteData()
是函数之一。在所有函数中,我捕获异常并记录它。现在由于这个
线程中止
异常,我得到了很多错误日志。我无法从调用WriteData()的函数中删除异常,因为我无法记录其他异常。IIS不是这样工作的…通常不会中止线程。。。(考虑添加证明您观点的链接,最好是MSDN或)我曾经遇到过线程中止异常,就像您在使用
response.redirect
时所说的,但如果我使用
HttpContext.Current.ApplicationInstance.CompleteRequest(),它不会引发任何异常。我过去每次收到例外情况都会收到电子邮件,所以有点烦人。所以,没有其他方法来修复或绕过这个问题?我自己的代码重定向了ThreadAbortedExceptions。我现在不怎么做网络工作了,但它过去确实是这样工作的。如果你在redirects
Response.Redirect(“example.aspx”,False)中使用这些代码;Response.Context.ApplicationInstance.CompleteRequest(),您不会得到任何异常。