2010-09-05 129 views
3

我以前沒有編寫過Windows服務,並且認爲一切都很順利,直到我部署到現場。在開發中,它工作正常,投票很棒,但一旦投入生產,它就會在第一次循環後落在背後。Windows服務的簡單線程問題

The exception I recieve is: 
Application: ProgramName.WinService.exe 
Framework Version: v4.0.30319 
Description: The process was terminated due to an unhandled exception. 
Exception Info: System.Exception 
Stack: 
    at ProgramName.WinService.UpdateChecker.StartChecks() 
    at ProgramName.WinService.UpdateChecker.StartPolling() 
    at System.Threading.ThreadHelper.ThreadStart_Context(System.Object) 
    at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
    at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
    at System.Threading.ThreadHelper.ThreadStart() 

這裏是做循環代碼:

 private readonly Thread pollingThread; 

    public UpdateChecker() 
    { 
     pollingThread = new Thread(StartPolling); 
     InitializeComponent(); 
    } 

     protected override void OnStart(string[] args) 
     { 
      pollingThread.Start(); 
     } 

     protected override void OnStop() 
     { 
      pollingThread.Abort(); 
     } 


     protected void StartPolling() 
     { 
      do 
      { 
       StartChecks(); 

       //10 seconds 
       Thread.Sleep(10000); 
      } while (true); 

     } 

沒有人有任何想法,爲什麼這會翻倒它運行後的第一次?我在做一些愚蠢的事情嗎?

這是造成問題的方法:

公共靜態字符串GetXmlFromFeed(串strUrl) { 變種rssReq = WebRequest.Create(strUrl); var rep = rssReq.GetResponse(); 返回新的StreamReader(rep.GetResponseStream())。ReadToEnd(); }

在的GetResponse()

可能超時,無關的線程在所有然後

+0

'StartChecks'內拋出異常;你能分享該方法的代碼嗎? – 2010-09-05 07:53:44

+1

一個方面的建議:我會改變.Abort()來設置一個布爾值bStopThreads = true,然後加入工作線程.Join() 如果超時然後調用.Abort()。在你的while循環中使用你的bStopThreads。 – hydrogen 2010-09-05 07:55:07

+0

任何內部異常? – cwap 2010-09-05 07:55:09

回答

1

望着異常堆棧跟蹤似乎StartChecks拋出一個異常,這是不處理並且它傳播到調用線程(this behavior was introduced in .NET 2.0,如之前在子線程中拋出的異常未被傳播)。

嘗試在其周圍放置一個try/catch以處理此異常。

+0

我想我只是需要一些嘗試抓住上述問題重試,這將是所有好去,謝謝所有 – JamesStuddart 2010-09-05 08:26:34