/// <summary> /// Wait until the first scheduled alarm is ready to be handled /// Assumes that we have a lock on the _schedule SortedSet /// </summary> private Time GetNextEvent() { // Wait for an alarm to be scheduled on the condition variable Count while (_schedule.Count == 0) { Monitor.Wait(_schedule); } // Once the alarm is scheduled, wait for the prescribed amount of time. // If a new alarm is scheduled with a shorter duration, Wait will preempt // and duration will update to reflect the new alarm's timestamp for (long duration = _timer.GetDuration(_schedule.First().TimeStamp); duration > 0; duration = _timer.GetDuration(_schedule.First().TimeStamp)) { Monitor.Wait(_schedule, TimeSpan.FromMilliseconds(duration)); } return(_schedule.Dequeue()); }