public void CancelPendingChanges() { lock (mChangeQueue) { mChangeQueue.Clear(); mQueueEvent.Reset(); } }
public ModeService(ServiceCreationInfo info) : base("mode", info) { mCurrentMode = new Mode("<none>"); mChangeQueue = new List <ModeChange>(); mQueueEvent = new AsyncEvent(); Task.Run(async() => { while (true) { // Retry once we have something in the queue. if (mChangeQueue.Count == 0) { await mQueueEvent.Wait(); continue; } ModeChange change; var delay = TimeSpan.Zero; lock (mChangeQueue) { change = mChangeQueue[0]; delay = change.ActivationTime - DateTime.Now; mQueueEvent.Reset(); if (delay <= TimeSpan.Zero) { mChangeQueue.RemoveAt(0); } } if (delay > TimeSpan.Zero) { using (var delayTokenSource = new CancellationTokenSource()) { var tasks = new Task[] { mQueueEvent.Wait(), Task.Delay(delay, delayTokenSource.Token) }; await Task.WhenAny(tasks); } continue; } CurrentMode = change.Mode; } }); }