Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Timer Interval Every 24 Hours

I'm following a tutorial on how to create a Windows Service that will send automated emails on my web server. I've got the tutorial working, however, the example code executes the service every 60mins, instead, I'd like the service executed once a day, every 24 hours, say at 9am every morning.

Below is the 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 the code which sets the interval to 60mins, however, am unsure what would I need to amend this to in order to make it run every 24 hours at 9am?

Any advice would be appreciated.

Thanks.

like image 248
tcode Avatar asked Jun 01 '15 08:06

tcode


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


1 Answers

You have couple of options:

  • Use Quartz .NET
  • Use Windows Schedule tasks

Don't rely on other timers as they will get out of sync in (near) future.

like image 120
oleksii Avatar answered Sep 24 '22 19:09

oleksii