public static void KillTimer(ITimerHanlder handler, int nTimerID) { int index = GetActiveTimerIndex(handler, nTimerID); //防止在OnTimer内移除循环Timer操作而导致的异常。 if (m_bIsOnTimer) { var timer = timers.Top.Value; int topTimerIndex = GetActiveTimerIndex(timer.m_handler, timer.m_timerID); if (index == topTimerIndex) {//执行次数设为1在OnTimer执行后移除 timer.m_timerCallCount = 1; return; } } if (index > -1) { uint priority = activeTimers[index].KeyTime; activeTimers.RemoveAt(index); timers.RemoveAtPriority(priority, (t => t.m_handler == handler && t.m_timerID == nTimerID)); } }
public TimerObject(ITimerHanlder handler, int nTimerID, uint nElapseTime, int nCallCount = -1) { m_handler = handler; m_timerID = nTimerID; m_elapseTime = nElapseTime; m_lasttime = Time.time; m_timerCallCount = nCallCount; }
/// <summary> /// 移除指定的定时器 /// </summary> /// <param name="handler"></param> /// <param name="timerId"></param> /// <returns></returns> private static bool RemoveActiveTimer(ITimerHanlder handler, int timerId) { int index = GetActiveTimerIndex(handler, timerId); if (index > -1) { activeTimers.RemoveAt(index); return(true); } return(false); }
/// <summary> /// 从活动定时器列表中获取指定定时器的序号 /// </summary> /// <param name="handler"></param> /// <param name="timerId"></param> /// <returns>-1:没在活动列表里</returns> private static int GetActiveTimerIndex(ITimerHanlder handler, int timerId) { for (int i = 0; i < activeTimers.Count; ++i) { if (activeTimers[i].Handler == handler && activeTimers[i].Id == timerId) { return(i); } } return(-1); }
public static bool SetTimer(ITimerHanlder handler, int nTimerID, float fElapseTime, int nCallCount = -1) { return(_addTimer(new TimerObject(handler, nTimerID, ToMilliSecond(fElapseTime), nCallCount))); }