开发者

How to be notified of date change in .NET WinForms application

I would like to specify a function to be called in my VB.Net 2005 WinForms project at midnight whe开发者_运维知识库n the system date changes. Is this possible?

I know I could do this by setting a timer by setting the interval to midnight minus the current time, then after the first tick, setting the interval to 24 hours. I was wondering if there was a cleaner, simpler way.

Edit to address comment: I would like to know when the day changes, for example from December 27th to December 28th. I know that the system date/time is continuously changing.


I don't like the timer/datediff idea for a couple of reasons. The most obvious is there isn't always 24 hours in a day and also you will need to restart the app periodically to resync to the clock. (similar to the patriot missle bug).

I would keep it simple and just hit the timer every second or minute or whatever the longest interval you can use for the accuracy required for your app is.

If you need it super accurate and can't trigger an event every millisecond I would hit it every minute until the last minute and then trigger another timer using the datediff.


You could instantiate a System.Threading.Timer with a period of 24 hours and with an initial time which is equal to the TimeSpan obtained by subtracting DateTime.Now from tomorrow's midnight (DateTime.Today.AddDays(1)).


Sounds like you could use regular Windows Task Sheduling.

This way you write a regular commandline application that works silently in the background when called. It could also exit if it's called before the right time.

Edit: In this case why not use a timer and be done with it? Even if you got the operating system to tell you the date, it would still be a software implementation (and not much more efficient than a .NET timer), so why not just implement it yourself?

Regarding cleanliness, it can look quite clean - create an event for it if you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜