static void RunScheduledAction(object state) { var s = (TimerState)state; s.Counter++; Console.WriteLine("{0} Running scheduled action {1}.", DateTime.UtcNow.TimeOfDay, s.Counter); if (s.ActionSchedule.StopDateTime != DateTime.MinValue && s.ActionSchedule.StopDateTime <= DateTime.UtcNow) { Console.WriteLine("disposing of timer..."); s.Timer.Dispose(); s.Timer = null; } else { AppRepository.RunAction(s.ActionSchedule.AppName, s.ActionSchedule.ActionName); var nextRunDate = DateTime.UtcNow.AddSeconds(s.ActionSchedule.IntervalAsSeconds()); //update timers due time to trigger the next run s.Timer.Change(nextRunDate - DateTime.UtcNow, TimeSpan.FromMilliseconds(-1)); //update schedule with next run date s.ActionSchedule.NextRun = nextRunDate; ActionScheduleRepository.SaveActionSchedule(s.ActionSchedule); } }
public List <ActionTimer> ScheduleAllActions() { var timers = new List <ActionTimer>(); var apps = AppRepository.GetInstalledApps(); foreach (var app in apps) { foreach (var action in app.Actions) { var schedule = ActionScheduleRepository.GetActionSchedule(app.AppName, action.ActionName); if (ShouldActionBeScheduled(schedule)) { var testTimer = new ActionTimer(schedule); timers.Add(testTimer); } } } return(timers); }