4

Recently I was trying to make a calendar application that will display the current year-month-date to the user. The problem is, if the user is gonna keep my application running even for the next day, how do I get notified ?? How shall I change the date displayed ? I don't wanna poll the current date to update it. Is this possible in c#.

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

5 Answers5

6

@OddThinking's answer will work (you could set a timer for the interval instead of sleeping). Another way would be to set a timer with a 1 minute interval and simply check if the system date has changed. Since you are only executing some lightweight code once a minute, I doubt the overhead would be noticable.

6
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;

}
Case
  • 111
  • 2
  • 7
4

Can you simply work out the number of seconds until midnight, and then sleep for that long?

Oddthinking
  • 24,359
  • 19
  • 83
  • 121
  • 3
    Well, some kind of timer might be preferable to a Sleep() [which would wipe out your UI] – Marc Gravell Nov 17 '08 at 10:18
  • Simple solution, but don't forget about daylight savings! Otherwise you'll wake up just past what you thought was midnight, but the date won't actually have changed. – Zebra North Nov 17 '08 at 11:24
  • I agree with Marc that a timer would be preferable to sleep. I had envisaged a separate thread, handling this, but that is probably overkill. Same idea though. – Oddthinking Nov 17 '08 at 12:21
  • Good point from Mr Zebra. Of course, it goes in both directions - you could wait too long, and leave the old date displaying for an extra hour. – Oddthinking Nov 17 '08 at 12:23
  • 2
    Also, the user might change the time in the middle (although you might handle this event separately) – splintor Oct 26 '11 at 07:08
2

Try looking into monitoring WMI events, you should be able to create a Wql event query that monitors the day of week change (i.e. ManagementEventWatcher etc) and then setup an event handler that fires when the 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));
    }
}
Student for Life
  • 1,023
  • 1
  • 9
  • 18
  • The year appears to be off by -1600 in the TimeEventArrived mthod (I got 12/28/0409 as the as the date, as opposed to 12/28/2009). Do you know why this would be? Do I just need to add 1600 to the year? – Charles Dec 28 '09 at 20:39
  • 1
    I would replace new DateTime(...) by DateTime.FromFileTimeUtc(...): this method uses January 1, 1601 as its "year 0", which is the correct year for the TIME_CREATED property – ckarras Jan 16 '10 at 22:48
0

How about a thread that checks for change in date. The thread can have some events that the controls that need this information can subscribe to.

Patrik Svensson
  • 13,536
  • 8
  • 56
  • 77