How to use the .NET Timer class to trigger an event at a specific time?

前端 未结 9 1677

I would like to have an event triggered in my app which runs continuously during the day at a certain time, say at 4:00pm. I thought about running the timer every second and

9条回答
  •  情深已故
    2020-12-01 00:31

    .NET has lots of timer classes, but they all take time spans relative to the current time. With a relative time, there are lots of things to consider before you start your timer and to monitor for while your timer is running.

    • What if the computer goes into a standby state?
    • What if the computer's time changes?
    • What if the expiration time is after a daylight savings time transition?
    • What if the user changes the computer's time zone after you started your timer such that there is a new time zone transition before expiration that wasn't previously there?

    The operating system is in a great place to handle this complexity. Application code running on .NET is not.

    For Windows, the NuGet package AbsoluteTimer wraps an operating system timer that expires at an absolute time.

提交回复
热议问题