[英]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.