I'm running into a problem with the System.Timers.Timer in a windows service. I basically set the timer up in a base polling service class with the following code:
_serviceTimer.Elapsed += OnElapsedTime;
_serviceTimer.Interval = ServiceTimerInterval.TotalMilliseconds;
_serviceTimer.AutoReset = false;
_serviceTimer.Enabled = true;
When the OnElapsedTime fires, I want to disable the timer and set the interval to a different value based on a lookup. The problem is when I change the interval it actually restarts the timer. This strange behavior is mentioned in the msndn docs with:
Note: If Enabled and AutoReset are both set to false, and the timer has previously been enabled, setting the Interval property causes the Elapsed event to be raised once, as if the Enabled property had been set to true. To set the interval without raising the event, you can temporarily set the AutoReset property to true. Timer.Interval
In the onelapsed event I have this:
_serviceTimer.Enabled = false;
double newIntervalSetting = newSetting;
base._serviceTimer.AutoReset = true;
base._serviceTimer.Interval = newIntervalSetting;
base._serviceTimer.AutoReset = false;
//reenable after processing
The problem is the interval change still begins the timer countdown and eventually fires off the event even though I set the autoreset to true prior to changing the interval. The enabled remains false at all times, but the event still fires. I'm not sure if I'm misinterpreting the msdn docs about the correct way to do this. Can anyone help me out?