/// <summary> /// Cancel a previously scheduled event. /// </summary> /// <param name="evt">The event to cancel</param> /// <returns>Returns true if the given event was successfully removed form the event queue</returns> public bool CancelEvent(IScheduledEvent evt) { if (_eventQueue.Remove(evt)) { Console.WriteLine($"{DateTime.Now} : Cancelled {evt.GetType().Name} previously scheduled @ {evt.ScheduledTime}"); return(true); } return(false); }
/// <summary> /// Schedule a new event by adding it to the event queue. /// </summary> /// <param name="evt">The event to schedule</param> /// <exception cref="InvalidOperationException">thrown if evt is in the past</exception> public void Schedule(IScheduledEvent evt) { if (evt.ScheduledTime <= DateTime.Now) { throw new InvalidOperationException("A passed event cannot be scheduled."); } _eventQueue.Enqueue(evt); Console.WriteLine($"{DateTime.Now} : Scheduled new {evt.GetType().Name} @ {evt.ScheduledTime}"); }
/// <summary> /// Called when update interval is elapsed. /// Check the queue to trigger the next event if required. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnTimerElapsed(object sender, ElapsedEventArgs e) { // While there is event that should be triggered at this time while (_eventQueue.Peek()?.ScheduledTime <= e.SignalTime) { IScheduledEvent evt = _eventQueue.Dequeue(); Console.WriteLine($"{DateTime.Now} : {evt.GetType().Name} triggered"); evt.Trigger(this); } }