7

I want to create a background worker for a WinForm that triggers code whenever midnight rolls by.

I have an idea of how to do it, but I'm pretty sure it's not the best way to do it.

while(1==1)
{
//if Datetime.Now == midnight, execute code
//sleep(1second)
}
sooprise
  • 22,657
  • 67
  • 188
  • 276
  • Quick note... If you ever want to create an "endless" loop like that (which could have a `break`, of course), you can do `while (true) { }`. `1==1` evaluates to `true` anyway. – Nelson Rothermel Jun 16 '10 at 13:46
  • Is that the accepted standard? I never was sure about the ok-ness of using 1==1 – sooprise Jun 16 '10 at 14:11

6 Answers6

23

Use a System.Timers.Timer and at application start up just calculate the difference between DateTime.Now and DateTime.Today.AddDays(0). Then set the interval for that amount.

I actually did something just like this recently:

public static class DayChangedNotifier
{
    private static Timer timer;

    static DayChangedNotifier()
    {
        timer = new Timer(GetSleepTime());
        timer.Elapsed += (o, e) =>
            {
                OnDayChanged(DateTime.Now.DayOfWeek);
                timer.Interval = this.GetSleepTime();
            };
        timer.Start();

        SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
    }

    private static void SystemEvents_TimeChanged(object sender, EventArgs e)
    {
        timer.Interval = GetSleepTime();
    }

    private static double GetSleepTime()
    {
        var midnightTonight = DateTime.Today.AddDays(1);
        var differenceInMilliseconds = (midnightTonight - DateTime.Now).TotalMilliseconds;
        return differenceInMilliseconds;
    }

    private static void OnDayChanged(DayOfWeek day)
    {
        var handler = DayChanged;
        if (handler != null)
        {
            handler(null, new DayChangedEventArgs(day));
        }
    }

    public static event EventHandler<DayChangedEventArgs> DayChanged;
}

AND:

public class DayChangedEventArgs : EventArgs
{
    public DayChangedEventArgs(DayOfWeek day)
    {
        this.DayOfWeek = day;
    }

    public DayOfWeek DayOfWeek { get; private set; }
}

Useage: DayChangedNotified.DayChanged += ....

BFree
  • 102,548
  • 21
  • 159
  • 201
  • @Soo: Nope. The Timer won't block (stopping the rest of your code -- http://en.wikipedia.org/wiki/Blocking_%28computing%29). Once the interval is up, it will call the `Tick` event. At that point you can stop the timer if you wish, calculate the next interval and start it up again. – Nelson Rothermel Jun 16 '10 at 13:47
  • No, the System.Timers fires on another thread so all you need to be careful about is to call Invoke in your event handler of the timer. – BFree Jun 16 '10 at 13:50
  • Or use a System.Windows.Forms.Timer instead, this one fires the Tick() event in the Form's thread. – Jürgen Steinblock Jun 16 '10 at 15:22
7

Instead you could user a Timer and set the timer tick interval to be the time between Now() and midnight.

AaronLS
  • 37,329
  • 20
  • 143
  • 202
1

I have no idea why polling solutions were voted up when Microsoft solved this type of problem years ago by adding a windows service to handle timing. Just create a scheduled task to run the exe. No extra overhead.

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
0

you can use Quartz to schedule that. Maybe is like a cannon to kill a mosquito in this scenario, but that's is the only scheduling job framework i know and works excellent.

Marcote
  • 2,977
  • 1
  • 25
  • 24
0

Don't use polling. Instead, set up a timer task, set it to fire at midnight, and add an event to process.

 TimeSpan timeBetween = DateTime.Today.AddDays(1) - DateTime.Now;

 System.Timers.Timer t = new System.Timers.Timer();
 t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
 t.Interval = 1000 * timeBetween.Seconds;
 t.Start();
cortijon
  • 983
  • 6
  • 13
0

I'm a little confuse about why you need a WinForm, will it be running at midnight? If all you need is some sort process to run, use the windows scheduler to run it at midnight. (On XP, but I believe Win server should be similar)Control Panel -> Scheduled Tasks -> Add Scheduled Task -> Fill out the wizard. Save you a lot of coding.

Chris L
  • 669
  • 2
  • 10
  • 21