public void CheckTimeTack() { for (int i = 0; i < tempTimeTaskList.Count; i++) { timeTaskList.Add(tempTimeTaskList[i]); } tempTimeTaskList.Clear(); for (int i = 0; i < timeTaskList.Count; i++) { CyTimeTask timeTask = timeTaskList[i]; if (GetNowUTCTime() < timeTask.FinishedTime) { continue; } Action action = timeTask.CallBack; action?.Invoke(); if (timeTask.Count == 1) { timeTaskList.RemoveAt(i); taskIDSet.Remove(timeTask.TaskID); i--; } else { timeTask.FinishedTime += timeTask.Delay; if (timeTask.Count != 0) { timeTask.Count--; } } } }
public int AddTimeTask(Action callBack, float delay, CyTimerUnit timerUnit = CyTimerUnit.Millisecond, int count = 1) { if (timerUnit != CyTimerUnit.Millisecond) { switch (timerUnit) { case CyTimerUnit.Second: delay = delay * 1000; break; case CyTimerUnit.Minute: delay = delay * 1000 * 60; break; case CyTimerUnit.Hour: delay = delay * 1000 * 60 * 60; break; case CyTimerUnit.Day: delay = delay * 1000 * 60 * 60 * 24; break; default: logCallBack?.Invoke("CyTimerUnit类型错误,进制转换失败!"); break; } } int taskID = GetTaskID(); double finishedTime = GetNowUTCTime() + delay; CyTimeTask timeTask = new CyTimeTask(taskID, callBack, finishedTime, delay, count >= 0 ? count : 1); tempTimeTaskList.Add(timeTask); taskIDSet.Add(taskID); return(taskID); }