C # timer interval Every 24 hours

I am following a tutorial on how to create a windows service that will send automatic emails on my web server. I have a tutorial, however the example code runs the service every 60 minutes, instead I would like the service to run once a day, every 24 hours, every day at 9am.

Below is a sample code

    private Timer scheduleTimer = null;
    private DateTime lastRun;
    private bool flag;

    public StarEmailService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("EmailSource"))
        {
            System.Diagnostics.EventLog.CreateEventSource("EmailSource", "EmailLog");
        }
        eventLogEmail.Source = "EmailSource";
        eventLogEmail.Log = "EmailLog";

        scheduleTimer = new Timer();
        scheduleTimer.Interval = 1 * 5 * 60 * 1000;
        scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);

    }

    protected override void OnStart(string[] args)
    {
        flag = true;
        lastRun = DateTime.Now;
        scheduleTimer.Start();
        eventLogEmail.WriteEntry("Started");
    }

    protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (flag == true)
        {
            ServiceEmailMethod();
            lastRun = DateTime.Now;
            flag = false;
        }
        else if (flag == false)
        {
            if (lastRun.Date < DateTime.Now.Date)
            {
                ServiceEmailMethod();
            }
        }
    }

      

The line scheduleTimer.Interval = 1 * 5 * 60 * 1000;

appears to be code that sets the interval to 60 minutes, however I'm not sure what I need to change so that it runs every 24 hours at 9am?

Any advice would be appreciated.

Thank.

+3


source to share


2 answers


You have several options:



Don't rely on other timers as they won't get syncronized in the near future.

+10


source


It's probably better to set the timer to a smaller interval and check the system time similar to your code. This code should send email once a day or after 9 am. The shorter your timer interval is, the more accurate it will be until 9am. For example, if you keep the timer interval at 60 minutes, the service checks the system time once an hour and the email will be sent between 9:00 am and 10:00 am. If you set the timer to 10 minutes, the service will check the system time every few minutes and send email from 9:00 am to 9:10 am.

This method does not sync up over time as it uses the system clock, not the timer interval, to know when to fire.

Remove the LastRun DateTime field and all references to it. Remove flag and link field. Add a DateTime field called nextRun:

private DateTime nextRun = DateTime.MinValue;

      



Add GetNextRun function:

private static DateTime GetNextRun(DateTime lastRun)
{
    var next = lastRun.AddDays(1);
    return new DateTime(next.Year, next.Month, next.Day, 9, 0, 0); 
}

      

Change ScheduleTimer expired before:

protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    if (DateTime.Now < nextRun) return;

    nextRun = GetNextRun(DateTime.Now);
    ServiceEmailMethod();
}

      

+3


source







All Articles