protected void Run() { while (!_finished) { // // Find out the next closest event in the sorted list. // BrunetTask task = null; bool fire = false; if (_task_queue.First == null) { task = (BrunetTask)_in_queue.Dequeue(); } else { _now_ticks = DateTime.UtcNow.Ticks; LinkedListNode <BrunetTask> next_node = _task_queue.First; if (next_node.Value.Instant > _now_ticks) { int milliseconds = (int)((next_node.Value.Instant - _now_ticks) / 10000.0); if (milliseconds < 0) { Environment.Exit(1); } //Console.WriteLine("millis:{0}", milliseconds); task = (BrunetTask)_in_queue.Dequeue(milliseconds, out fire); } else { fire = true; } } if (fire) { // // Time to fire the next event in the queue. // task = _task_queue.First.Value; _task_queue.RemoveFirst(); //Console.WriteLine("Firing event"); task.Fire(); } else { // // Add the new task to the sorted list. // if (task != null) { AddTask(task); } } } }
protected void AddTask(BrunetTask task) { // // Classical linked list insertion. // LinkedListNode <BrunetTask> current = _task_queue.Last; while (current != null) { if (task.Instant > current.Value.Instant) { _task_queue.AddAfter(current, task); break; } current = current.Previous; } if (current == null) { _task_queue.AddFirst(task); } }
public void Schedule(BrunetTask task) { _in_queue.Enqueue(task); }
protected void AddTask(BrunetTask task) { // // Classical linked list insertion. // LinkedListNode<BrunetTask> current = _task_queue.Last; while (current != null) { if (task.Instant > current.Value.Instant) { _task_queue.AddAfter(current, task); break; } current = current.Previous; } if (current == null) { _task_queue.AddFirst(task); } }