Пример #1
0
        public void Update()
        {
            Profiler.Start("scheduler");
            Profiler.Start("scheduler.receive-events");
            long start = Stopwatch.ElapsedTicks;
            long limit = Stopwatch.ElapsedMilliseconds + 10;

            while (ImmediateEventQueue.Count > 0 && Stopwatch.ElapsedMilliseconds < limit)
            {
                ScheduledEvent e;
                bool           dequeued = false;
                while (!(dequeued = ImmediateEventQueue.TryDequeue(out e)) &&
                       Stopwatch.ElapsedMilliseconds < limit)
                {
                    ;
                }
                if (dequeued)
                {
                    ScheduleEvent(e);
                }
            }
            while (LaterEventQueue.Count > 0 && Stopwatch.ElapsedMilliseconds < limit)
            {
                ScheduledEvent e;
                bool           dequeued = false;
                while (!(dequeued = LaterEventQueue.TryDequeue(out e)) &&
                       Stopwatch.ElapsedMilliseconds < limit)
                {
                    ;
                }
                if (dequeued)
                {
                    ScheduleEvent(e);
                }
            }
            Profiler.Done();
            Profiler.Start("scheduler.dispose-subjects");
            while (DisposedSubjects.Count > 0 && Stopwatch.ElapsedMilliseconds < limit)
            {
                IEventSubject subject;
                bool          dequeued = false;
                while (!(dequeued = DisposedSubjects.TryDequeue(out subject)) &&
                       Stopwatch.ElapsedMilliseconds < limit)
                {
                    ;
                }
                if (dequeued)
                {
                    // Cancel all events with this subject
                    for (int i = 0; i < Events.Count; i++)
                    {
                        if (Events[i].Subject == subject)
                        {
                            Events.RemoveAt(i);
                            i--;
                        }
                    }
                    Subjects.Remove(subject);
                }
            }
            limit = Stopwatch.ElapsedMilliseconds + 10;
            Profiler.Done();
            for (int i = 0; i < Events.Count && Stopwatch.ElapsedMilliseconds < limit; i++)
            {
                var e = Events[i];
                if (e.When <= start)
                {
                    Profiler.Start("scheduler." + e.Name);
                    e.Action(Server);
                    Events.RemoveAt(i);
                    i--;
                    Profiler.Done();
                }
                if (e.When > start)
                {
                    break;                     // List is sorted, we can exit early
                }
            }
            Profiler.Done(20);
        }
Пример #2
0
        public void Update()
        {
            Profiler.Start("scheduler");
            Profiler.Start("scheduler.receive-events");
            var start = Stopwatch.ElapsedTicks;
            var limit = Stopwatch.ElapsedMilliseconds + 10;

            while (ImmediateEventQueue.Count > 0 && Stopwatch.ElapsedMilliseconds < limit)
            {
                ScheduledEvent e;
                bool           dequeued;
                while (!(dequeued = ImmediateEventQueue.TryDequeue(out e)) &&
                       Stopwatch.ElapsedMilliseconds < limit)
                {
                }

                if (dequeued)
                {
                    ScheduleEvent(e);
                }
            }

            while (LaterEventQueue.Count > 0 && Stopwatch.ElapsedMilliseconds < limit)
            {
                ScheduledEvent e;
                bool           dequeued;
                while (!(dequeued = LaterEventQueue.TryDequeue(out e)) &&
                       Stopwatch.ElapsedMilliseconds < limit)
                {
                }

                if (dequeued)
                {
                    ScheduleEvent(e);
                }
            }

            Profiler.Done();
            Profiler.Start("scheduler.dispose-subjects");
            while (DisposedSubjects.Count > 0 && Stopwatch.ElapsedMilliseconds < limit)
            {
                IEventSubject subject;
                bool          dequeued;
                while (!(dequeued = DisposedSubjects.TryDequeue(out subject)) &&
                       Stopwatch.ElapsedMilliseconds < limit)
                {
                }

                if (dequeued)
                {
                    // Cancel all events with this subject
                    for (var i = 0; i < Events.Count; i++)
                    {
                        if (Events[i].Subject == subject)
                        {
                            Events.RemoveAt(i);
                            i--;
                        }
                    }

                    Subjects.Remove(subject);
                }
            }

            limit = Stopwatch.ElapsedMilliseconds + 10;
            Profiler.Done();
            for (var i = 0; i < Events.Count && Stopwatch.ElapsedMilliseconds < limit; i++)
            {
                var e = Events[i];
                if (e.When <= start)
                {
                    Profiler.Start("scheduler." + e.Name);

                    if (!Constants.IgnoredEvents.Contains(e.Name))
                    {
                        var subjectLine = e.Subject != null ? $" on subject {e.Subject?.GetType().Name}" : string.Empty;
                        Server.Trace.TraceEvent(TraceEventType.Verbose, 0, $"activating event '{e.Name}'{subjectLine}");
                    }

                    e.Action(Server);
                    Events.RemoveAt(i);
                    i--;
                    Profiler.Done();
                }

                if (e.When > start)
                {
                    break;                     // List is sorted, we can exit early
                }
            }

            Profiler.Done(20);
        }