Is there a way to get notification of date change in c#?
I have a requirement where I have to do something when the system date is changed.
I found that SystemsEvent.TimeChanged is an event you can hook into, however it is only fired only when user has changed the time.
Would this be better handled by having a scheduled task/cron job that runs at midnight?
You could implement this (admittedly poorly) as follows:
Write a very lightweight class which simply stores the current date. Start it in a separate thread and make it periodically check the date, then sleep for a large amount of time (in fact, you could make it sleep for just longer than the time left until midnight). If the date has changed, make it call a callback function in the main part of your app.
Like I said, this doesn't seem like a brilliant solution to your problem and there are probably much better ways of doing it.
You might be looking for System.Threading.Timer. It will fire at an interval you set.
The system broadcasts a WM_TIMECHANGE message when the user changes the system date/time. The correct way to handle this is to handle that message. I suspect the .net event SystemEvents.TimeChanged is just a wrapper around this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With