Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service System.Timers.Timer not firing

I have a Windows service written in C# which is meant to perform a task every few minutes. I'm using a System.Timers.Timer for this but it doesn't ever appear to fire. I've looked at many different posts here on SO and elsewhere and I'm not seeing what is wrong with my code.

Here is my code, with non-timer related items removed for clarity...

namespace NovaNotificationService {     public partial class NovaNotificationService : ServiceBase     {         private System.Timers.Timer IntervalTimer;         public NovaNotificationService()         {             InitializeComponent();             IntervalTimer = new System.Timers.Timer(60000);  // Default in case app.config is silent.             IntervalTimer.Enabled = false;             IntervalTimer.Elapsed += new ElapsedEventHandler(this.IntervalTimer_Elapsed);         }          protected override void OnStart(string[] args)         {             // Set up the timer...             IntervalTimer.Enabled = false;             IntervalTimer.Interval = Properties.Settings.Default.PollingFreqInSec * 1000;             // Start the timer and wait for the next work to be released...             IntervalTimer.Start();         }          protected override void OnStop()         {             IntervalTimer.Enabled = false;         }          private void IntervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)         {   // Do the thing that needs doing every few minutes...             DoWork();         }     } } 

I'm really scratching my head over this one. Can anybody spot what silly thing I'm getting wrong?

EDIT: By suggestion, I added IntervalTimer.Enabled = true; before IntervalTimer.Start(); in the service OnStart method. This doesn't resolve the issue.

I've added file trace logging into the service to confirm some of the internals and I know for sure that the Timer.Enabled value is true by the time OnStart() is finished.

like image 593
Joel Brown Avatar asked Dec 11 '11 19:12

Joel Brown


People also ask

Does system timers timer run in a separate thread?

Yes, they run in a different thread.

Is system threading timer thread safe?

Timer is not thread-safe.

How does system timer work?

Remarks. The Timer component is a server-based timer that raises an Elapsed event in your application after the number of milliseconds in the Interval property has elapsed. You can configure the Timer object to raise the event just once or repeatedly using the AutoReset property.


1 Answers

Here is my work-around...

After way too many hours searching for an answer to this, I discovered a wide variety of articles and blogs discussing timers in Windows services. I've seen a lot of opinions on this and they all fall into three categories and in descending order of frequency:

  1. Don't use System.Windows.Forms.Timer because it won't work. (this only makes sense)

  2. Don't use System.Threading.Timer because it doesn't work, use System.Timers.Timer instead.

  3. Don't use System.Timers.Timer because it doesn't work, use System.Threading.Timer instead.

Based on this, I tried 2. This is also the approach that seems to be recommended by Microsoft since they say that System.Timers.Timer is suited to "Server applications".

What I've found is that System.Timers.Timer just doesn't work in my Windows Service application. Therefore I've switched over to System.Threading.Timer. It's a nuisance since it requires some refactoring to make it work.

This is approximately what my working code looks like now...

namespace NovaNotificationService {     public partial class NovaNotificationService : ServiceBase     {         private System.Threading.Timer IntervalTimer;         public NovaNotificationService()         {             InitializeComponent();         }          protected override void OnStart(string[] args)         {             TimeSpan tsInterval = new TimeSpan(0, 0, Properties.Settings.Default.PollingFreqInSec);             IntervalTimer = new System.Threading.Timer(                 new System.Threading.TimerCallback(IntervalTimer_Elapsed)                 , null, tsInterval, tsInterval);         }          protected override void OnStop()         {             IntervalTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);             IntervalTimer.Dispose();             IntervalTimer = null;         }          private void IntervalTimer_Elapsed(object state)         {   // Do the thing that needs doing every few minutes...             // (Omitted for simplicity is sentinel logic to prevent re-entering             //  DoWork() if the previous "tick" has for some reason not completed.)             DoWork();         }     } } 

I hate the "Doctor, doctor, it hurts when I do this..." solution, but that's what I had to resort to. One more opinion on the pile for the next guy with this problem...

like image 137
Joel Brown Avatar answered Sep 30 '22 21:09

Joel Brown