Receive notification when datetime changes in C #

Recently, I tried to create a calendar application that will display the current year-month to the user. The problem is that if a user continues to work on my application even the next day, how do I get a notification? How to change the displayed date? I do not want to poll the current date to update it. Is this possible in C #.

Note. I tried the SystemEvent.TimeChanged event, but it only works if the user manually changes the time and date from the control panel.

+4
source share
5 answers

Can you just tell the number of seconds before midnight and then sleep a long time?

+4
source

@OddThinking answer will work (you can set a timer for the interval instead of sleep). Another way is to set a timer with an interval of 1 minute and just check to see if the system date has changed. Since you only run small light code once a minute, I doubt that the overhead will be noticeable.

+6
source
public void Main() { var T = new System.Timers.Timer(); T.Elapsed += CallBackFunction; var D = (DateTime.Today.AddDays(1).Date - DateTime.Now); T.Interval = D.TotalMilliseconds; T.Start(); } private void CallBackFunction(object sender, System.Timers.ElapsedEventArgs e) { (sender as System.Timers.Timer).Interval = (DateTime.Today.AddDays(1).Date - DateTime.Now).TotalMilliseconds; } 
+5
source

Try examining WMI events, you should be able to create a Wql event request that tracks the day of the week (i.e. ManagementEventWatcher, etc.), and then set up an event handler that fires when an event arrives.

  using System;
 using System.Management;

 class program
 {
     public static void Main ()
     {
         WqlEventQuery q = new WqlEventQuery ();
         q.EventClassName = "__InstanceModificationEvent";
         q.Condition = @ "TargetInstance ISA 'Win32_LocalTime' AND TargetInstance.Hour = 22 AND TargetInstance.Minute = 7 AND TargetInstance.Second = 59";

         Console.WriteLine (q.QueryString);

         using (ManagementEventWatcher w = new ManagementEventWatcher (q))
         {
             w.EventArrived + = new EventArrivedEventHandler (TimeEventArrived);
             w.Start ();
             Console.ReadLine ();  // Block this thread for test purposes only ....
             w.Stop ();
         }
     }

     static void TimeEventArrived (object sender, EventArrivedEventArgs e)
     {
         Console.WriteLine ("This is your wake-up call");
         Console.WriteLine ("{0}", new
         DateTime ((long) (ulong) e.NewEvent.Properties ["TIME_CREATED"]. Value));
     }
 }
+1
source

How about a thread that checks for a date change. There may be some events in the stream that controls can be subscribed to that need this information.

0
source

All Articles