大家好,我现在碰到IIS abort 在网页中开启的线程的问题.
我的情况如下:在一个页面中,用户点击button,开启一个线程去别的网站抓数据,写到自己的库里.大概的代码如下:
GetInvoiceThread = new Thread(new ParameterizedThreadStart(LogOnAndGetInvoiceReport));
GetInvoiceThread.IsBackground = true;
GetInvoiceThread.Start(customer_Year);
LogOnAndGetInvoiceReport:用来抓数据,比较耗时.
如果数据很多,这个线程会执行1小时以上,很多时候此线程被abort了. Error message如下:
1
RequestDownloadInvoiceReportCSVFileNew Error.
Exception Message:Thread was being aborted.
Exception Source:System
Exception StackTrace:
at System.Net.ConnectStream.System.Net.IRequestLifetimeTracker.TrackRequestLifetime(Int64 requestStartTimestamp)
at System.Net.HttpWebRequest.GetResponse()
at Ethos.MBB.Business.RequestsTeleNor.RequestDownloadInvoiceReportCSVFileNew(String url, Guid customerGuid) in D:\Ethos_Project\Mytos
\SourceCode\trunk\Business\Ethos.MBB.Business\TeleNor\RequestsTeleNor.cs:line 655
2
RequestDownloadInvoiceReportCSVFileNew Error.
Exception Message:The read operation failed, see inner exception.
Exception Source:System
Exception StackTrace:
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.IO.StreamReader.ReadBuffer() at
System.IO.StreamReader.ReadLine()
at Ethos.MBB.Business.RequestsTeleNor.RequestDownloadInvoiceReportCSVFileNew(String url, Guid customerGuid) in D:\Ethos_Project\Mytos
\SourceCode\trunk\Business\Ethos.MBB.Business\TeleNor\RequestsTeleNor.cs:line 613
每次出现的错误的原因不一样,但是结果都是这个线程被abort了.
我采取了补救措施,起了一个listen线程,用来监听FetchData线程,如果FetchData线程Isactive= false或者 Fetchdata线程是null,就重新启动Fetchdata线程
继续抓数据.Listen线程功能很简单,代码如下:
while(true)
{
if (GetReportsTeleNor.IsBusy == true && GetReportsTeleNor.IsGetInvoiceFinished == false)
{//IsBusy:begin fetch, IsGetInvoiceFinished: fetch finish
if ((GetReportsTeleNor.GetInvoiceThread != null && GetReportsTeleNor.GetInvoiceThread.IsAlive == false) ||
(GetReportsTeleNor.GetInvoiceThread == null))
{
new Ethos.Mytos.DataAccess.FetchLogManager().FetchLogAdd(new Ethos.Mytos.Entity.FetchLog
{
flog_CustID = Customer_id,
flog_Summary = "Restart fetch thread, IsBusy:" + GetReportsTeleNor.IsBusy.ToString()
+ ",Finished:" +
GetReportsTeleNor.IsGetInvoiceFinished.ToString(),
flog_Message = "Restart fetch thread, IsBusy:" + GetReportsTeleNor.IsBusy.ToString()
+ ",Finished:" +
GetReportsTeleNor.IsGetInvoiceFinished.ToString()
});
GetReportsTeleNor.IsBusy = false;
uxFetchData_Click(null, null); //重新启动FetchData线程
}
}
Thread.Sleep(10000);
}
启动Listen线程的方法:
timerThread = new Thread(timerProc);
timerThread.IsBackground = true;
timerThread.Start();
试验结果我发现Fetchdata线程和Listen线程会一起被abort。 Listen线程错误信息如下
Exception Message:Thread was being aborted.
Exception Source:mscorlib
Exception StackTrace:
at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
at MBBWebRole.CustomerManage.timerProc() in D:\Ethos_Project\Mytos\SourceCode\trunk\Portal\MBBWebRole\CustomerManage.aspx.cs:line 467
所以我怀疑是IIS把Fetchdata线程和Listen线程都杀死了。请问有什么解决办法么?