C# Timer Interval Every 24 Hours -
i'm following tutorial on how create windows service send automated emails on web server. i've got tutorial working, however, example code executes service every 60mins, instead, i'd service executed once day, every 24 hours, @ 9am every morning.
below 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 code sets interval 60mins, however, unsure need amend in order make run every 24 hours @ 9am?
any advice appreciated.
thanks.
you have couple of options:
- use quartz .net
- use windows schedule tasks
don't rely on other timers out of sync in (near) future.
Comments
Post a Comment