Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get notification of date change in c#?

Tags:

date

c#

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.

like image 641
Praveen Sharma Avatar asked Dec 16 '08 19:12

Praveen Sharma


4 Answers

Would this be better handled by having a scheduled task/cron job that runs at midnight?

like image 51
Harper Shelby Avatar answered Nov 02 '22 23:11

Harper Shelby


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.

like image 20
xan Avatar answered Nov 03 '22 01:11

xan


You might be looking for System.Threading.Timer. It will fire at an interval you set.

like image 38
Darryl Braaten Avatar answered Nov 03 '22 00:11

Darryl Braaten


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.

like image 33
Will Rickards Avatar answered Nov 03 '22 01:11

Will Rickards