void IUpdateable.Update() { int i; for (i = 0; i < scheduledItems.Count; i++) { ScheduledItem item = scheduledItems[i]; if (loopTimeService.Time < item.ExecutionTime) { break; } item.Execute(); if (!item.Repeating) { continue; } item.Reschedule(loopTimeService.Time + item.Schedule); scheduledItems.RemoveAt(i); scheduledItems.InsertOrdered(item, comparer); i--; } if (i > 0) { scheduledItems.RemoveRange(0, i); } }
/// <summary> /// Schedules the specified task to be invoked on a specified schedule, after an optional delay. /// </summary> /// <param name="task">The task/action to be run.</param> /// <param name="schedule">The delay between invocations.</param> /// <param name="delay">An additional delay for the first time run.</param> /// <returns>A disposable object representing the scheduled task. Calling Dispose() will cancel the schedule and any future invocations.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if the schedule is less than 0.</exception> /// <exception cref="ArgumentNullException">Thrown if no action is specified.</exception> public IDisposable ScheduleRepeating(Action task, TimeSpan schedule, TimeSpan delay = default) { if (schedule <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(delay), $"{nameof(delay)} cannot be <= zero."); } if (task == null) { throw new ArgumentNullException(nameof(task)); } Log.Debug($"Scheduled Repeating Task: {task.Method.GetFullName()}"); ScheduledItem item = new ScheduledItem(this, task, loopTimeService.Time + delay.TotalSeconds + schedule.TotalSeconds, schedule.TotalSeconds); scheduledItems.InsertOrdered(item, comparer); return(item); }
internal void Unschedule(ScheduledItem scheduledItem) { scheduledItems.Remove(scheduledItem); }