Ejemplo n.º 1
0
        public void Close()
        {
            if (m_TimerAxis == null)
            {
                return;
            }

            for (int i = 0; i < m_TimerAxis.Count; ++i)
            {
                ArrayListEx timeList = m_TimerAxis[i] as ArrayListEx;
                int         nSize    = timeList.Count;
                for (int j = 0; j < nSize; ++j)
                {
                    TimerInfo time = timeList.Get(i) as TimerInfo;
                    if (time != null)
                    {
                        KillTimer(time.dwTimerID, time.handler);
                    }
                }

                timeList.Clear();
            }
            m_TimerAxis.Clear();
            m_TimerAxis = null;
        }
Ejemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////////////////////////////////
        //private bool FindTimer(uint timerID, ITimer handler)
        //{
        //    Dictionary<uint, TimerInfo> timer = null;
        //    if (!m_Timers.TryGetValue(handler, out timer))
        //    {
        //        //Debuger.LogError("删除不存在的定时器");
        //        return false;
        //    }

        //    TimerInfo info = null;
        //    if (!timer.TryGetValue(timerID, out info))
        //    {
        //        //Debuger.LogError("删除不存在的定时器TimerID:{0}", timerID);
        //        return false;
        //    }

        //    return true;
        //}

        public void CheckTimer()
        {
            if (m_TimerAxis == null)
            {
                return;
            }

            uint now = (uint)TimeHelper.GetTickCount();

            if (now - m_dwLastCheckTick < CHECK_FREQUENCY)
            {
                return;
            }

            uint start_grid = (uint)(((m_dwLastCheckTick - m_dwInitializeTime) / TIME_GRID) % m_TimerAxis.Count);
            uint cur_grid   = (uint)(((now - m_dwInitializeTime) / TIME_GRID) % m_TimerAxis.Count);

            m_dwLastCheckTick = now;

            uint i = start_grid;

            // 遍历时间刻度
            do
            {
                // 遍历当前时间刻度中的所有待触发定时器
                ArrayListEx timeList = m_TimerAxis[(int)i] as ArrayListEx;
                for (int t = 0; t < timeList.Count; ++t)
                {
                    TimerInfo timer = timeList.Get(t) as TimerInfo;
                    if (timer == null)
                    {
                        continue;
                    }

                    // 触发定时器
                    if (now - timer.dwLastCallTick >= timer.dwInterval)
                    {
                        //uint dwTick = (uint)TimeHelper.GetTickCount();

                        // 回调定时器
                        //Profiler.BeginSample("OnTimer");
                        try
                        {
                            timer.handler.OnTimer(timer.dwTimerID);
                        }
                        catch (System.Exception ex)
                        {
                            Utility.Log.Error(ex.ToString());
                            //continue;
                        }

                        //Profiler.EndSample();
                        if (IsExist(timer.dwTimerID, timer.handler))
                        {
                            // 性能测试代码
                            //uint nCostTime = (uint)TimeHelper.GetTickCount() - dwTick;
                            //if(nCostTime > 64)
                            //{
                            //}

                            timer.dwLastCallTick = now;
                            if (timer.dwCallTimes != INFINITY_CALL)
                            {
                                timer.dwCallTimes -= 1;
                            }

                            if (timer.dwCallTimes == 0)
                            {
                                // 调用次数已经够了
                                KillTimer(timer.dwTimerID, timer.handler);
                            }
                            else
                            {
                                // 搬迁到下一次触发的位置
                                timer.dwGridIndex = (uint)(((timer.dwLastCallTick + timer.dwInterval - m_dwInitializeTime) / TIME_GRID) % m_TimerAxis.Count);
                                timeList.Remove(t);

                                ArrayListEx newTimeList = m_TimerAxis[(int)timer.dwGridIndex] as ArrayListEx;
                                timer.timeAixsPos = newTimeList.Add(timer);
                            }
                        }
                        else
                        {
                            //Engine.Utility.Log.Error("Remove Time {0} {1}", timer.dwTimerID, timer.handler.ToString());
                            timer = null;
                            timeList.Remove(t);
                        }
                    }
                }

                // 递进到下一个刻度
                if (i == cur_grid)
                {
                    break;
                }

                i = (uint)((i + 1) % m_TimerAxis.Count);
            }while(i != cur_grid);
        }