System.Timers.Timer and Daylight Savings

C

chrisdevey

Is there any way to make a System.Timers.Timer adjust for daylight
savings time change? In a long running process I set a timer as follows
for a daily expiration:

_myTimer = new Timer(_myTimerDelegate, null, nextExpiration,
TimeSpan.FromDays(1));

When we make the change out of daylight time back to standard time, the
timer appears to fire one hour early (e.g., 4pm instead of 5pm). Is
there a way to set this timer so that it is daylight-time aware?

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

This isn't a timer issue. The timer has no concept of the current time.
It only knows that in a ^span^ of time, it has to fire.

Because of this, you will have to check the timer period every time it
is fired, and determine if the next day will result in a shift in the time
because of daylight savings.

A better solution would be to use the Windows Scheduler to run this
task. It can be configured to run at a certain time each day, and is not
affected by daylight savings. Because the OS updates the time automatically
for daylight savings, the scheduler, which is based on the system time, will
work correctly.

Hope this helps.
 
C

chrisdevey

Thanks, that's what I thought. This function is actually sending a
message periodically from a long running process, so the Scheduler
would probably not be an option. It's too bad there's not an option on
the Timer to adjust for daylight changes, ie increment/decrement the
span when it crosses a daylight boundary.
 
N

Nicholas Paldino [.NET/C# MVP]

What is stopping you from stopping the timer and creating a new instance
with the correct time span?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top