/** * @purpose : 删除定时器 * @param timerID : 定时器ID * @param handler : 处理接口 * @return : 返回是否删除成功 */ public bool KillTimer(uint timerID, ITimer handler) { if (handler == null) { return(false); } 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); } if (m_TimerAxis == null) { return(false); } ArrayListEx timeList = m_TimerAxis[(int)info.dwGridIndex] as ArrayListEx; if (timeList != null) { // 清空时间轴上的列表 timeList.Remove(info.timeAixsPos); } timer.Remove(timerID); if (timer.Count <= 0) { m_Timers.Remove(handler); } return(true); }
/////////////////////////////////////////////////////////////////////////////////////////////////// //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); }