views:

1362

answers:

5

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.

+4  A: 

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

Oddthinking
Well, some kind of timer might be preferable to a Sleep() [which would wipe out your UI]
Marc Gravell
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.
MrZebra
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
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
+2  A: 

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
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
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
+5  A: 

@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.

A: 

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