/// <summary> /// Adds an entry to the schedule using ScheduleItem property values. /// </summary> /// <param name="name">(Optional) Name or comment describing the scheduled item. Can be searched to retrieve ScheduleItem object.</param> /// <param name="recurring">True if the ScheduleItem should recur at a stated interval.</param> /// <param name="interval">If the ScheduleItem is set to recur, the interval between scheduled events.</param> /// <param name="scheduledAt">The next (or first) date\time when the ScheduleItem reached event will fire.</param> /// <param name="onScheduleAt">The callback method to execute when the scheduledAt time is reached. This method's signature is myMethod(object sender, ScheduleItem e);</param> /// <returns>The created ScheduleItem. The Id value will be populated, since it was added to the schedule.</returns> public static ScheduleItem AddToSchedule(string name, bool recurring, TimeSpan interval, DateTime scheduledAt, OnScheduledAtDelegate onScheduleAt, object state) { ScheduleItem newItem = new ScheduleItem(name, recurring, interval, scheduledAt, onScheduleAt, state); newItem.Id = Guid.NewGuid(); scheduleList.Add(newItem); return newItem; }
/// <summary> /// Create a new empty Schedule object. Do not populate the Id field, it is set when added to the schedule. /// </summary> public ScheduleItem() { _id = Guid.Empty; _name = null; _recurring = false; _interval = new TimeSpan(); _scheduledAt = new DateTime(); _onScheduledAt = null; _state = null; }
/// <summary> /// Create a new ScheduleItem object and populate its fields. /// </summary> /// <param name="name">(Optional) The name is available for readability and it is possible to search on name.</param> /// <param name="recurring">Set to True for a recurring ScheduleItem.</param> /// <param name="interval">For recurring ScheduleItems, this is the amount of time to elapse between recurrances. If no scheduledAt value is provided first occurance with be at a timespan of interval in the future.</param> /// <param name="scheduledAt">The device time when the 1st occurance of the schedule should trigger.</param> public ScheduleItem(string name, bool recurring, TimeSpan interval, DateTime scheduledAt, OnScheduledAtDelegate eventHandler, object status) { ValidateFieldValues(name, recurring, interval, scheduledAt, eventHandler); _id = Guid.Empty; _name = name; _recurring = recurring; _interval = interval; if (scheduledAt != DateTime.MinValue) _scheduledAt = scheduledAt; else _scheduledAt = DateTime.Now.Add(interval); _onScheduledAt = eventHandler; _state = status; }
internal void ValidateFieldValues(string name, bool recurring, TimeSpan interval, DateTime scheduledAt, OnScheduledAtDelegate eventHandler) { TimeSpan emptyTimeSpan = new TimeSpan(); DateTime emptyDateTime = DateTime.MinValue; if (interval == emptyTimeSpan && scheduledAt == emptyDateTime) throw new ArgumentException("No time specified for next scheduled event; supply either interval or nextEvent parameter."); if (scheduledAt != emptyDateTime && scheduledAt <= DateTime.Now) throw new ArgumentException("Invalid time specified for next scheduled event."); if (recurring && interval == emptyTimeSpan) throw new ArgumentException("No recurring interval specified for recurring schedule item."); if (eventHandler == null) throw new ArgumentException("Non-servicable SchduleItem, the event handler delegate is null."); }