Note that there are some explanatory texts on larger screens.

plurals
  1. POTimer behavior when execution takes longer than span?
    primarykey
    data
    text
    <p>I'm writing windows service which will process "something" every couple minutes.</p> <p>Here is some code:</p> <pre><code>public Service() { this.InitializeComponent(); this.ServiceName = Name; this.CanPauseAndContinue = true; this.CanShutdown = true; this.eventLog.Source = Name; // initialize timer this.timer.Elapsed += this.TimerElapsed; } private void TimerElapsed(object sender, ElapsedEventArgs e) { eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information); if (this.processor.PrepareToRun()) { this.processor.Run(); } } </code></pre> <p>I wonder what will happen if <code>this.processor.Run()</code> will take long time and next <code>TimerElapsed</code> event will be raised? Will it skip? Will it wait and run ASAP after finished? Should I consider those scenarios and code for them?</p> <p>I'm using <code>System.Timers.Timer</code></p> <p><strong>EDIT</strong>:</p> <pre><code>private void TimerElapsed(object sender, ElapsedEventArgs e) { eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information); try { this.timer.Stop(); if (this.processor.PrepareToRun()) { this.processor.Run(); } } catch (Exception ex) { LoggingAndNotifications.LogAndNotify(ex); } finally { this.timer.Start(); } } </code></pre> <p><strong>EDIT 2</strong></p> <pre><code>public Service() { this.InitializeComponent(); this.ServiceName = Name; this.CanPauseAndContinue = true; this.CanShutdown = true; this.eventLog.Source = Name; // initialize timer this.timer.AutoReset = false; this.timer.Elapsed += this.TimerElapsed; } private void TimerElapsed(object sender, ElapsedEventArgs e) { eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information); try { if (this.processor.PrepareToRun()) { this.processor.Run(); } } catch (Exception ex) { LoggingAndNotifications.LogAndNotify(ex); throw; } finally { this.timer.Start(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload