i'm writing application that triggers once weekly. set due timer go off sundays @ 5pm. interval however, how can reset go off following sunday @ 5pm?
for example if started application on sunday @ 4:59:50pm first callback right @ expected 5pm, since need set interval set approximately 5pm following sunday.
2 techniques i'm avoiding: 1. can manually type in (1000 * 60 * 60 * 24 * 7) in order interval week on timer, due timers being based on timer ticks , not real time know potentially cause program fall short few seconds each week. 2. @ end of callback method can recall scheduler procedure.
what want reset interval approximately set following sunday @ 5pm recalling code within scheduler method after callback sent out. method may still suffer loss time due ticks, since reset each week in code margin of error on time smaller doing hardcoded interval , code cleaner using practice of recalling scheduler callback method.
public void myscheduler() { // trigger initial time first day of week (sunday) @ 5pm datetime markedtime = datetime.today.adddays(-(int)datetime.today.dayofweek).addhours(17); //if not sunday, increment next sunday if (datetime.now > markedtime) { markedtime = markedtime.adddays(7); } // initialize callback , set start time, set interval mytimer = new system.threading.timer(sender => callbackmethod(variables passed), null, (int)(markedtime - datetime.now).totalmilliseconds,timeout.infinite); }
one of favourite libraries fluentscheduler. simplifies scheduling tasks quite bit me. in case guess following work fine:
public class myregistry : registry { public myregistry() { schedule(() => { // }).torunevery(1).weeks().on(dayofweek.sunday).at(17, 0); } } you can on nuget
install-package fluentscheduler
Comments
Post a Comment