private void FireEvent(DLEventType evetType, object data)
 {
     if (OnEventFired != null)
     {
         OnEventFired.Invoke(this, new DLEventArgs(evetType, data));
     }
 }
Beispiel #2
0
        /// <summary>
        /// Processes the queue and fires events.
        /// </summary>
        private void QueueProcessing()
        {
            TimeSpan waitForNewTimeOut = TimeSpan.Zero;

            while (!_cancellationToken.IsCancellationRequested)
            {
                lock (_eventsAvailableLock) {
                    // wait until we're flagged that there are events available.
                    Monitor.Wait(_eventsAvailableLock, waitForNewTimeOut > TimeSpan.Zero ? waitForNewTimeOut : Scheduler._defaultProcessWaitTime);

                    // reset time to wait.
                    waitForNewTimeOut = TimeSpan.Zero;

                    lock (_queueLock) {
                        if (_priorityQueue.First == null)
                        {
                            // no more items on the queue, go to wait.
                            Console.WriteLine($"{nameof(Scheduler)}: Queue empty.");
                            continue;
                        }

                        // store a single 'current' time for processing of all items in the queue
                        // TODO: use 'current' time from Game.Instance
                        var currentTimeInMilliseconds = GetMillisecondsAfterReferenceTime(DateTime.Now);

                        // check the current queue and fire any events that are due.
                        while (_priorityQueue.Count > 0)
                        {
                            // the first item always points to the next-in-time event available.
                            var nextEvent = _priorityQueue.First;

                            // check if this event has been cancelled.
                            if (_cancelledEvents.Contains(nextEvent.EventId))
                            {
                                // dequeue, clean and move next.
                                _priorityQueue.Dequeue();
                                CleanAllAttributedTo(nextEvent.EventId, nextEvent.RequestorId);
                                continue;
                            }

                            // check if the event is due
                            if (nextEvent.Priority <= currentTimeInMilliseconds)
                            {
                                // actually dequeue this item.
                                _priorityQueue.Dequeue();

                                OnEventFired?.Invoke(this, new EventFiredEventArgs(nextEvent));
                                continue;
                            }

                            // else the next item is in the future, so figure out how long to wait, update and break.
                            waitForNewTimeOut = TimeSpan.FromMilliseconds(nextEvent.Priority < currentTimeInMilliseconds ? 0 : nextEvent.Priority - currentTimeInMilliseconds);
                            break;
                        }
                    }
                }
            }
        }