public void Advance(TimeSpan offset)
{
_now = _now.Add(offset);
var tickItems = _scheduledWork.Where(s => s.Key <= _now.Ticks).OrderBy(s => s.Key).ToList();
foreach (var t in tickItems)
{
foreach (var si in t.Value.Where(i => i.Cancelable == null || !i.Cancelable.IsCancellationRequested))
{
if (si.Type == ScheduledItem.ScheduledItemType.Message)
si.Receiver.Tell(si.Message, si.Sender);
else
si.Action();
si.DeliveryCount++;
}
Queue<ScheduledItem> removed;
_scheduledWork.TryRemove(t.Key, out removed);
foreach (var i in removed.Where(r => r.Repeating && (r.Cancelable == null || !r.Cancelable.IsCancellationRequested)))
{
InternalSchedule(null, i.Delay, i.Receiver, i.Message, i.Action, i.Sender, i.Cancelable, i.DeliveryCount);
}
}
}