![](/img/trans.png)
[英]How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download
[英]Exception: Thread was being aborted when not dealing with a Response.End issue
我有一個網絡應用程序,該應用程序每隔30分鍾就會調用一個頁面來激活工作人員。 每周大約5-7次,我得到一個Thread was being aborted
異常Thread was being aborted
aspx看起來像這樣:
<%@ Page Language="C#" AutoEventWireup="true" %>
<html>
<head runat="server">
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
System.Threading.Tasks.Task.Factory
.StartNew(() => { new MyApplicationWorker().RunApplicationWorker(); });
}
</script>
</head>
<body></body>
</html>
在Web.config中,我有這行代碼來防止超時,並且我知道工作線程完成的時間要短得多。
<httpRuntime enableVersionHeader="false" executionTimeout="300" />
如您所見,頁面將加載並調用在后台運行的工作程序。 沒有Response.End問題或Server.Transfer,這是大多數其他處理此問題的內容。 我該如何防止異常發生?
因此,這是我一年來一直在尋找的錯誤,所有其他相關問題似乎都在處理Response.End()
問題,但實際情況並非如此。
我一直以為發生錯誤的方法有問題,但實際上是工人被告知錯誤開始的地方。
解決方案很簡單:我添加了.Wait()
,從那時起該錯誤就從日志中消失了。
System.Threading.Tasks.Task.Factory
.StartNew(() => {
new MyApplicationWorker().RunApplicationWorker().Wait();
});
感謝alex.b的評論導致了另一個問題 ,我希望這可以幫助其他將遇到同一問題的工作線程中止的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.