//------------------------------------------------------------------------- /// <summary> /// 创建Timer /// </summary> /// <param name="nTimerID"></param> /// <param name="nIntervalTime"></param> /// <param name="del"></param> /// <param name="nCallTime"></param> public void CreateTimer(uint uTimerID, uint uIntervalTime, DelITimerHander del, uint uCallTime = 0xffffffff, string strInfo = "") { if (null == del) { return; } if (uIntervalTime == 0) { uIntervalTime = 1; } //Debug.Log("CTimerSystem::CreateTimer del.GetHashCode() = " + del.GetHashCode()); int nHashCode = del.GetHashCode(); List <uint> singleList = null; if (m_TimerDict.TryGetValue(nHashCode, out singleList)) { bool isExists = singleList.Exists(delegate(uint p) { return(p == uTimerID); }); if (isExists) { return; } singleList.Add(uTimerID); } else { singleList = new List <uint>(); singleList.Add(uTimerID); m_TimerDict.Add(nHashCode, singleList); } CTimerHandle sTimerHandle = new CTimerHandle(); sTimerHandle.m_Handle = del; sTimerHandle.m_unTimerID = uTimerID; sTimerHandle.m_unCallTimes = uCallTime; sTimerHandle.m_unIntervalTime = uIntervalTime; sTimerHandle.m_unLastCallTick = m_unLastCheckTick; sTimerHandle.m_nHandleHashCode = nHashCode; if (!string.IsNullOrEmpty(strInfo)) { sTimerHandle.m_strDebugInfo = strInfo; } sTimerHandle.m_unTimerGridIndex = ((sTimerHandle.m_unLastCallTick + sTimerHandle.m_unIntervalTime - m_unInitializeTime) / gs_DEFAULT_TIME_GRID) % m_unTimerAxisSize; List <CTimerHandle> axisList = null; if (!m_TimerAxis.TryGetValue(sTimerHandle.m_unTimerGridIndex, out axisList)) { axisList = new List <CTimerHandle>(); m_TimerAxis.Add(sTimerHandle.m_unTimerGridIndex, axisList); } if (null != axisList) { axisList.Add(sTimerHandle); } }
//------------------------------------------------------------------------- /// <summary> /// 根据时间标识更新 /// </summary> /// <param name="uKey"></param> /// <param name="unCurTick"></param> private void __UpdateByKey(uint uKey, uint unCurTick) { List <CTimerHandle> axisList = null; if (m_TimerAxis.TryGetValue(uKey, out axisList)) { if (null == axisList || 0 == axisList.Count) { m_TimerAxis.Remove(uKey); axisList = null; } } if (null == axisList) { return; } CTimerHandle sTimerTemp = null; for (int nIndex = axisList.Count - 1; nIndex >= 0; --nIndex) { sTimerTemp = axisList[nIndex]; if (!__CheckByHashCode(sTimerTemp.m_nHandleHashCode, sTimerTemp.m_unTimerID)) { axisList.RemoveAt(nIndex); continue; } if (m_unLastCheckTick - sTimerTemp.m_unLastCallTick >= sTimerTemp.m_unIntervalTime) { uint dwTick = __GetTickTime(); if (null != sTimerTemp.m_Handle) { sTimerTemp.m_Handle(sTimerTemp.m_unTimerID); } if (sTimerTemp == axisList[nIndex]) { uint nCostTime = __GetTickTime() - dwTick; if (nCostTime > 64 && nCostTime > gs_DEFAULT_TIME_GRID) { Debug.Log("CTimerSystem::__UpdateByKey - 定时器频率过低: ID = " + sTimerTemp.m_unTimerID); } sTimerTemp.m_unLastCallTick = unCurTick; sTimerTemp.m_unCallTimes -= 1; if (0 == sTimerTemp.m_unCallTimes) { DestroyTimer(sTimerTemp.m_unTimerID, sTimerTemp.m_Handle); } else { uint unNewGrid = ((sTimerTemp.m_unLastCallTick + sTimerTemp.m_unIntervalTime - m_unInitializeTime) / gs_DEFAULT_TIME_GRID) % m_unTimerAxisSize; if (sTimerTemp.m_unTimerGridIndex == unNewGrid) { continue; } sTimerTemp.m_unTimerGridIndex = unNewGrid; axisList.RemoveAt(nIndex); List <CTimerHandle> axisListNew = null; if (!m_TimerAxis.TryGetValue(sTimerTemp.m_unTimerGridIndex, out axisListNew)) { axisListNew = new List <CTimerHandle>(); m_TimerAxis.Add(sTimerTemp.m_unTimerGridIndex, axisListNew); } if (null != axisListNew) { axisListNew.Add(sTimerTemp); } } } else { axisList.RemoveAt(nIndex); continue; } } } }
//------------------------------------------------------------------------- /// <summary> /// 创建Timer /// </summary> /// <param name="nTimerID"></param> /// <param name="nIntervalTime"></param> /// <param name="del"></param> /// <param name="nCallTime"></param> public void CreateTimer(uint uTimerID, uint uIntervalTime, DelITimerHander del, uint uCallTime = 0xffffffff, string strInfo = "") { if (null == del) { return; } if( uIntervalTime == 0 ) uIntervalTime = 1; Debug.Log("CTimerSystem::CreateTimer del.GetHashCode() = " + del.GetHashCode()); int nHashCode = del.GetHashCode(); List<uint> singleList = null; if (m_TimerDict.TryGetValue(nHashCode,out singleList)) { bool isExists = singleList.Exists(delegate(uint p) { return p == uTimerID; }); if (isExists) { return; } singleList.Add(uTimerID); } else { singleList = new List<uint>(); singleList.Add(uTimerID); m_TimerDict.Add(nHashCode, singleList); } CTimerHandle sTimerHandle = new CTimerHandle(); sTimerHandle.m_Handle = del; sTimerHandle.m_unTimerID = uTimerID; sTimerHandle.m_unCallTimes = uCallTime; sTimerHandle.m_unIntervalTime = uIntervalTime; sTimerHandle.m_unLastCallTick = m_unLastCheckTick; sTimerHandle.m_nHandleHashCode = nHashCode; if (!string.IsNullOrEmpty(strInfo)) { sTimerHandle.m_strDebugInfo = strInfo; } sTimerHandle.m_unTimerGridIndex = ((sTimerHandle.m_unLastCallTick + sTimerHandle.m_unIntervalTime - m_unInitializeTime) / gs_DEFAULT_TIME_GRID) % m_unTimerAxisSize; List<CTimerHandle> axisList = null; if (!m_TimerAxis.TryGetValue(sTimerHandle.m_unTimerGridIndex,out axisList)) { axisList = new List<CTimerHandle>(); m_TimerAxis.Add(sTimerHandle.m_unTimerGridIndex, axisList); } if (null != axisList) { axisList.Add(sTimerHandle); } }