2016-05-25 9 views
-1

私は2つのタイマーを実行するWindowsサービスを持っています.1つは24時間に15分間隔で実行します。 イベントロギングを追加して、経過イベントハンドラが15分に発生しているように見えますが、そうではないように見えます。誰もがこのコードで間違ったものを見ることができますか?Windowsサービスが経過イベントハンドラを起動しない

public partial class GTstaging : ServiceBase 
    { 
     System.Timers.Timer regularTimer = new System.Timers.Timer(); 
     System.Timers.Timer longTimer = new System.Timers.Timer(); 
     DateTime _scheduleTime; 

     public staging() 
     { 
      InitializeComponent(); 
      _scheduleTime = DateTime.Today.AddDays(1).AddHours(Convert.ToDouble(ConfigurationManager.AppSettings["ScheduleTime_" + DateTime.Now.DayOfWeek.ToString()])); 
     } 

     protected override void OnStart(string[] args) 
     { 
      //** original - ConsoleApplication1.Program.DoProcessing(); 

      using (EventLog eventLog = new EventLog("Application")) 
      { 
       eventLog.Source = "Application"; 
       eventLog.WriteEntry("START, regular timer:" + ConfigurationManager.AppSettings["RegularTimer"], EventLogEntryType.Information, 101, 1); 
      } 

      this.regularTimer.Enabled = true; 
      this.regularTimer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["RegularTimer"]); 
      this.regularTimer.AutoReset = true; 
      this.regularTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoRegular); 

      this.longTimer.Enabled = true; 
      this.longTimer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000; 
      this.longTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoLongRunning); 

     } 

     private void DoRegular(object sender, System.Timers.ElapsedEventArgs e) 
     { 
//do stuff then log 
       using (EventLog eventLog = new EventLog("Application")) 
       { 
        eventLog.Source = "Application"; 
        eventLog.WriteEntry("Regular Process End", EventLogEntryType.Information, 101, 1); 
       } 
     } 

     private void DoLongRunning(object sender, System.Timers.ElapsedEventArgs e) 
     { 
      //do stuff 
     } 

     protected override void OnStop() 
     { 
      this.regularTimer.Stop(); 
      this.longTimer.Stop(); 
      this.regularTimer = null; 
      this.longTimer = null; 
     } 

    } 
} 
+0

'ConfigurationManager.AppSettings [" RegularTimer "]'をeventLogに書き込むときの設定は何ですか? –

+0

こんにちは、それは900000 – DarkW1nter

+1

イベントログではなく 'DoRegular'イベントハンドラでファイルに書き込もうとしたのだろうかと思います。私はあなたが2つの異なるスレッド(サービスのメインスレッドとタイマーイベントハンドラ)でログにアクセスしていると思います。だからあなたの出来事は発砲しているかもしれませんが、ロギングのためにあなたは偽陰性を見ています。 [このMSDNのページを確認](https://msdn.microsoft.com/en-us/library/0680sfkd.aspx) –

答えて

1

私はこの方法をデバッグします:

this.longTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoLongRunning); 

  1. は、デバッグモードでアプリケーションを実行し、ちょうどタイマーが 設定されていることを確認する、行を次のようにブレークポイントを置きます

  2. 以前のBreakPointが届いている場合は、タイマ間隔を5と10 秒に設定し、正常に動作することをテストしてからでアプリケーションを実行してくださいデバッグモード:

    this.regularTimer.Interval = 5000; //Convert.ToDouble(ConfigurationManager.AppSettings["RegularTimer"]); 
    
        this.longTimer.Interval = 10000; //_scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000; 
    

    が続いDoLongRunning方法でDoRegular方法と別にブレークポイントを入れて、あなたはDoRegular方法は OnStartMethod後、いくつかの秒に達していることがわかり、あなたが表示された場合はデバッグモード

  3. でアプリケーションを実行しますそのDoLongRunningメソッドは にも達します。this.regularTimer.Intervalthis.longTimer.Intervalのコードは、元のソースコード で正しく設定する必要があります。

    this.longTimer.Elapsed BreakPoint が届いていれば、その値を確認できます。 (覚えている値はミリ秒単位で表現される)

私は良い一日を、あなたのコードを修正し、このように願っています!

関連する問題