2011-01-07 21 views
2

我正在創建另一個Windows服務,並且我的計時器沒有打勾,我不知道爲什麼! 我正在使用system.timers.timer,因爲我在以前的服務中,它不起作用。 我試着附加它,但它似乎沒有做任何事情。System.Timers.Timer timer1_Elapsed未開火!幫幫我!

我的代碼:

namespace ExpiryNotifier 
{ 
    public partial class ExpiryNotifier : ServiceBase 
    { 
     public ExpiryNotifier() 
     { 
      InitializeComponent(); 
      if (!System.Diagnostics.EventLog.SourceExists("ExpiryNotifier")) 
      { 
       System.Diagnostics.EventLog.CreateEventSource("ExpiryNotifier", "ExpiryNotifier"); 
      } 
      eventLog1.Source = "ExpiryNotifier"; 
      eventLog1.Log = "ExpiryNotifier"; 
     } 
     private Timer timer1 = new Timer(); 
     protected override void OnStart(string[] args) 
     { 
      eventLog1.WriteEntry("Service Started"); 
      timer1.Elapsed += timer1_Elapsed; 
      timer1.Interval = 10000; 
      timer1.Enabled = true; 

     } 

     protected override void OnStop() 
     { 
      eventLog1.WriteEntry("Service Stopped"); 
      timer1.Enabled = false; 

     } 

     private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
     { 
      eventLog1.WriteEntry("timer tick"); 
      timer1.Stop(); 

      EmailerService.EmailerService service = new EmailerService.EmailerService(); 
      DataSet expiringQualifications = service.GetDetailsOfExpiringQualifications(); 

      if(expiringQualifications.Tables[0].Rows.Count>0) 
      { 
       foreach(DataRow rw in expiringQualifications.Tables[0].Rows) 
       { 
        if (!string.IsNullOrEmpty(rw["EmailAddress"].ToString())) 
        { 
         if (rw["QualAwardDescription"] != null) 
         { 
          service.SendQualExpiryEmail(rw["EmailAddress"].ToString(), rw["firstName"].ToString(), 
                 rw["QualAwardDescription"].ToString()); 
         } 
        } 
       } 
      } 


      timer1.Start(); 
     } 
    } 
} 

任何人都可以看到這個問題?

在此先感謝!

貝克斯

+1

你能看到「Service Started」條目嗎? –

回答

7

System.Timers.Timer是難看的定時器。它所做的一件令人討厭的事情是由Elapsed事件處理程序引發的吞食異常。由於您在進入該方法時停止計時器,它會殺死您的計時器。沒有任何通知,它只是停止工作。

您必須至少爲此代碼添加異常處理,以便您可以記錄異常並停止服務。

還要注意OnStart()方法中的錯誤,每次服務啓動時都會一直添加一個事件處理函數。 Elapsed事件運行多次,本身就是一種很好的方式來轟炸一些東西。

考慮System.Threading.Timer,它沒有任何這些問題。

+0

感謝您提醒我該錯誤。我把它移動到初始化,現在服務已經開始工作。初始化會好嗎? – Bex

+0

如果有效,那就沒關係。僅在OnStart/Stop中更改已啓用。並添加異常處理。 –

0

System.Timers.Timer.Start()具有相同的功能,System.Timers.Timer.Enabled = TRUE; System.Timers.Timer.Stop()與System.Timers.Timer.Enabled = false具有相同的功能;

這兩種方法在內部將Enabled屬性設置爲true或false,從而啓動或停止計時器。

檢查您是否有權寫入eventlog。您還可以檢查事件日誌中的錯誤。

System.Timers.Timer不是線程安全的。確保您正確使用它。

0

它看起來像它的工作,但你可能是一個更好的方式來調試它。誰知道也許你eventLog1是空

更新您的OnStart包括這個

protected override void OnStart(string[] args) 
    { 
     foreach (string arg in args) 
     { 
      if (arg == "DEBUG_SERVICE") 
        DebugMode(); 

     } 

    #if DEBUG 
     DebugMode(); 
    #endif 

    eventLog1.WriteEntry("Service Started"); 
    timer1.Elapsed += timer1_Elapsed; 
    timer1.Interval = 10000; 
    timer1.Enabled = true; 

    } 

private static void DebugMode() 
{ 

    Debugger.Break(); 
} 

現在,當你點擊開始,你會得到一個對話框,詢問是否要附加服務。它的方式更容易,然後嘗試手動附加。

+0

我設法讓服務打勾,但我無法調試它。 當我附加它時,即使它寫入事件日誌,也沒有中斷點,所以我知道它在做這件事,如果我添加了debug.break,它會導致服務崩潰! – Bex

+0

不要緊,它引用.net 4客戶端配置文件而不是完整版本,這似乎是我無法調試的原因。 – Bex