コード例 #1
0
        protected void QueueUpdateCallback(object state)
        {
            try
            {
                if (!_running || Interlocked.CompareExchange(ref _currentUpdateThreadId,
                                                             Thread.CurrentThread.ManagedThreadId, 0) != 0)
                {
                    return;
                }
                long elapsedMilliseconds1 = m_queueTimer.ElapsedMilliseconds;
                int  dt = (int)(elapsedMilliseconds1 - m_lastUpdate);
                m_lastUpdate = (int)elapsedMilliseconds1;
                foreach (IUpdatable updatable in m_updatables)
                {
                    try
                    {
                        updatable.Update(dt);
                    }
                    catch (Exception ex)
                    {
                        LogUtil.ErrorException(ex, "Failed to update: " + updatable);
                    }
                }

                for (int index = m_timers.Count - 1; index >= 0; --index)
                {
                    SimpleTimerEntry timer = m_timers[index];
                    if (GetDelayUntilNextExecution(timer) <= 0)
                    {
                        try
                        {
                            timer.Execute(this);
                        }
                        catch (Exception ex)
                        {
                            LogUtil.ErrorException(ex, "Failed to execute timer: " + timer);
                        }
                    }
                }

                IMessage message;
                while (m_messageQueue.TryDequeue(out message))
                {
                    try
                    {
                        message.Execute();
                    }
                    catch (Exception ex)
                    {
                        LogUtil.ErrorException(ex, "Failed to execute message: " + message);
                    }

                    if (!_running)
                    {
                        return;
                    }
                }

                long elapsedMilliseconds2 = m_queueTimer.ElapsedMilliseconds;
                long num = elapsedMilliseconds2 - elapsedMilliseconds1 > (long)m_updateInterval
          ? 0L
          : elapsedMilliseconds1 + m_updateInterval - elapsedMilliseconds2;
                Interlocked.Exchange(ref _currentUpdateThreadId, 0);
                if (_running)
                {
                    _updateTask = Task.Factory.StartNewDelayed((int)num,
                                                               QueueUpdateCallback, this);
                }
            }
            catch (Exception ex)
            {
                LogUtil.ErrorException(ex, "Failed to run TaskQueue callback for \"{0}\"", (object)Name);
            }
        }
コード例 #2
0
        protected void ProcessCallback(object state)
        {
            if (IsRunning && Interlocked.CompareExchange(ref m_currentThreadId, System.Threading.Thread.CurrentThread.ManagedThreadId, 0) == 0)
            {
                long num = 0L;
                try
                {
                    num = m_queueTimer.ElapsedMilliseconds;
                    int dtMillis = (int)(num - (long)m_lastUpdate);
                    m_lastUpdate = (int)num;
                    foreach (TimerEntry current in m_timers)
                    {
                        try
                        {
                            current.Update(dtMillis);
                        }
                        catch (Exception argument)
                        {
                            // SelfRunningTaskPool.logger.Error<TimerEntry, Exception>("Failed to update {0} : {1}", current, argument);
                        }
                        if (!IsRunning)
                        {
                            return;
                        }
                    }
                    int count = m_simpleTimers.Count;
                    int num2  = count - 1;
                    while (num2 >= 0)
                    {
                        SimpleTimerEntry simpleTimerEntry = m_simpleTimers[num2];
                        if (GetDelayUntilNextExecution(simpleTimerEntry) > 0)
                        {
                            goto IL_107;
                        }
                        try
                        {
                            simpleTimerEntry.Execute(this);
                            goto IL_107;
                        }
                        catch (Exception argument)
                        {
                            //    SelfRunningTaskPool.logger.Error<SimpleTimerEntry, Exception>("Failed to execute timer {0} : {1}", simpleTimerEntry, argument);
                            goto IL_107;
                        }
IL_FF:
                        num2--;
                        continue;
IL_107:
                        if (!IsRunning)
                        {
                            return;
                        }
                        goto IL_FF;
                    }
                }
                catch (Exception argument)
                {
                    //  SelfRunningTaskPool.logger.Error<string, Exception>("Failed to run TaskQueue callback for \"{0}\" : {1}", Name, argument);
                }
                finally
                {
                    long elapsedMilliseconds = m_queueTimer.ElapsedMilliseconds;
                    bool flag;
                    long num3 = (flag = (elapsedMilliseconds - num > (long)UpdateInterval)) ? 0L : (num + (long)UpdateInterval - elapsedMilliseconds);
                    Interlocked.Exchange(ref m_currentThreadId, 0);
                    if (flag)
                    {
                        //     SelfRunningTaskPool.logger.Debug<string, long>("TaskPool '{0}' update lagged ({1}ms)", Name, elapsedMilliseconds - num);
                    }
                    if (IsRunning)
                    {
                        m_updateTask = Task.Factory.StartNewDelayed((int)num3, new Action <object>(ProcessCallback), this);
                    }
                    else
                    {
                        m_stoppedAsync.Set();
                    }
                }
            }
        }