[英]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
异常Server cannot append header after HTTP headers have been sent
, 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
}
}
所有这些代码都在function(可以说) writeData()
,并由一个称为CallWriteData
的函数调用。 现在,该异常已在WriteData()
函数中成功处理,但其抛出Thread was being aborted
在父函数CallWriteData
Thread was being aborted
异常CallWriteData
。
老实说,这不是我项目中的主要问题,但是如果我解决这个令人讨厌的问题,那将很好。 同样, CallWriteData
此异常并非每次都存在(有时可以成功处理)。
最后,这帮助我处理了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
异常Server cannot append header after HTTP headers have been sent
获取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表示“已发送退货,请杀死它。” 就是这样。
(该线程可能已保存以供其他请求重用,但是刚刚完成的代码被中止了。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.