private void RemoveTimeListItem(int index) { if (timeTaskList.Count == 0 && tempTimeTaskList.Count == 0) { return; } TimeTask task = timeTaskList[index]; RemoveListItem_TimeTask(timeTaskList, index); if (index < timeTaskList.Count) { TimeTask indexTask = timeTaskList[index]; TaskFlag flag = new TaskFlag { id = indexTask.id, index = index, active = true }; idDic[indexTask.id] = flag; } delIds.Add(task.id); //lock(lockId) idDic.Remove(task.id); }
private void TimeTaskTick() { if (tempTimeTaskList.Count > 0) { lock (lockTime) { for (int i = 0; i < tempTimeTaskList.Count; i++) { AddTimeListItem(tempTimeTaskList[i]); } tempTimeTaskList.Clear(); } } nowTime = GetUTCMilliseconds(); for (int i = 0; i < timeTaskList.Count; i++) { TimeTask task = timeTaskList[i]; if (nowTime.CompareTo(task.destTime) < 0) { continue; } else { Action <int> cb = task.callBack; try { if (cb != null && taskHandle != null) { taskHandle.Invoke(cb, task.id); } else if (cb != null) { cb.Invoke(task.id); } } catch (Exception e) { LogInfo(e.ToString(), LogLevel.Error); } } if (task.count == 1) { RemoveTimeListItem(i); ///*lock (lockTime) */deleteIds.Add(task.id); } else { if (task.count > 0) { --task.count; } task.destTime += task.delay; timeTaskList[i] = task; } } }
private void RemoveListItem_TimeTask(List <TimeTask> list, int index) { int last = list.Count - 1; TimeTask temp = list[index]; list[index] = list[last]; list[last] = temp; list.RemoveAt(last); }
private void AddTimeListItem(TimeTask task) { TaskFlag flag = new TaskFlag { id = task.id, index = timeTaskList.Count, active = true }; idDic[task.id] = flag; timeTaskList.Add(task); }
public bool ReplaceTimeTask(int id, Action <int> callBack, double delay, int count = 1, TimeUnit unit = TimeUnit.Millisecound) { switch (unit) { case TimeUnit.Millisecound: break; case TimeUnit.Secound: delay *= 1000; break; case TimeUnit.Minute: delay *= 1000 * 60; break; case TimeUnit.Hour: delay *= 1000 * 60 * 60; break; case TimeUnit.Day: delay *= 1000 * 60 * 60 * 24; // 最大支持 24天 break; } nowTime = GetUTCMilliseconds(); double destTime = nowTime + delay; TimeTask task = new TimeTask { id = id, delay = delay, destTime = destTime, callBack = callBack, count = count, }; if (idDic.ContainsKey(id) && idDic[id].active) { timeTaskList[idDic[id].index] = task; return(true); } else { for (int i = 0; i < tempTimeTaskList.Count; i++) { if (tempTimeTaskList[i].id == id) { tempTimeTaskList[i] = task; return(true); } } } return(false); }
public IDPack AddTimerTask(Action <int> callBack, double delay, int count = 1, TimeUnit unit = TimeUnit.Millisecound) { switch (unit) { case TimeUnit.Millisecound: break; case TimeUnit.Secound: delay *= 1000; break; case TimeUnit.Minute: delay *= 1000 * 60; break; case TimeUnit.Hour: delay *= 1000 * 60 * 60; break; case TimeUnit.Day: delay *= 1000 * 60 * 60 * 24; // 最大支持 24天 break; } nowTime = GetUTCMilliseconds(); double destTime = nowTime + delay; int id = GetId(); if (id == -1) { return(new IDPack(id, TaskType.TimeTask)); } idDic[id] = new TaskFlag(idDic[id], TaskType.TimeTask); TimeTask task = new TimeTask { id = id, delay = delay, destTime = destTime, callBack = callBack, count = count }; lock (lockTime) { tempTimeTaskList.Add(task); } return(new IDPack(id, TaskType.TimeTask)); }