public int LoopOnce() { if (this.TaskQueue.Count == 0) { return(0); } if (this.ReadyTasks.Count > 0) { throw new InvalidOperationException("Concurrent call is not allowed"); } object locker = this.Locker; lock (locker) { DateTime utcNow = DateTime.UtcNow; LinkedListNode <Looper.LooperTask> linkedListNode = this.TaskQueue.First; while (linkedListNode != null) { Looper.LooperTask value = linkedListNode.Value; if (value.When <= utcNow) { this.ReadyTasks.Add(value); LinkedListNode <Looper.LooperTask> node = linkedListNode; linkedListNode = linkedListNode.Next; this.TaskQueue.Remove(node); } else { linkedListNode = linkedListNode.Next; } } } int count = this.ReadyTasks.Count; if (count == 0) { return(0); } for (int i = 0; i < count; i++) { this.ReadyTasks[i].Action(); } this.ReadyTasks.Clear(); return(count); }
public void Schedule(Action action, DateTime when, bool atFrontQueue) { Assert.NotNull(action, "action"); object locker = this.Locker; lock (locker) { Looper.LooperTask value = new Looper.LooperTask(action, when); if (atFrontQueue) { this.TaskQueue.AddFirst(value); } else { this.TaskQueue.AddLast(value); } } }