/// <summary> /// Gets the renew date based on the given frequency /// </summary> /// <param name="actualTriggerDate">Actual date that the task ran</param> /// <param name="repeat">The repeat frequency</param> /// <returns></returns> private DateTime GetRenewDate(DateTime actualTriggerDate, TaskSchedulerRepeatOnceA repeat) { try { if (repeat == TaskSchedulerRepeatOnceA.Week) { return(GetNextWeekday(actualTriggerDate, actualTriggerDate.DayOfWeek)); } else if (repeat == TaskSchedulerRepeatOnceA.Day) { return(actualTriggerDate.AddDays(1)); } else if (repeat == TaskSchedulerRepeatOnceA.Hour) { return(actualTriggerDate.AddHours(1)); } else if (repeat == TaskSchedulerRepeatOnceA.HafHour) { return(actualTriggerDate.AddMinutes(30)); } else { throw new Exception("Renew mode '" + repeat + "' not found."); } } catch (Exception e) { Log("Fail to get new run date. - " + e.Message, e); return(actualTriggerDate); } }
/// <summary> /// Schedules a new task /// </summary> /// <param name="runDateTime">The date that the task should run</param> /// <param name="taskFunc">The function that the task will run</param> /// <param name="repeat">The repeating time of the task. Use it to auto renew the task.</param> /// <returns>Returns the task id</returns> public ulong ScheduleTask(DateTime runDateTime, Func <Task> taskFunc, TaskSchedulerRepeatOnceA repeat = TaskSchedulerRepeatOnceA.Never) { ulong taskId = GetNextId(); ScheduledTask temp = new ScheduledTask(runDateTime, taskFunc, repeat, taskId); temp.Scheduler.AutoReset = false; _scheduledTasks.Add(temp); return(taskId); }