如何处理被中止的线程异常vb.net/C#?
我已经看到了一些关于这个问题的问题,但我还没有找到一个适合我的问题的答案。 最初,我在函数中编写json后使用以下代码如何处理被中止的线程异常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(); 正在获
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。我现在不怎么做网络工作了,但它过去确实是这样工作的。如果你在redirectsResponse.Redirect(“example.aspx”,False)中使用这些代码;Response.Context.ApplicationInstance.CompleteRequest()代码>,您不会得到任何异常。