public bool ReplaceTimeTask(int tid, Action <int> callback, float delay, KDTimeUnit timeUnit = KDTimeUnit.Millisecond, int count = 1) { if (timeUnit != KDTimeUnit.Millisecond) { switch (timeUnit) { case KDTimeUnit.Second: delay = delay * 1000; break; case KDTimeUnit.Minute: delay = delay * 1000 * 60; break; case KDTimeUnit.Hour: delay = delay * 1000 * 60 * 60; break; case KDTimeUnit.Day: delay = delay * 1000 * 60 * 60 * 24; break; default: LogInfo("Replace Task TimeUnit Type Error..."); break; } } nowTime = GetUTCMilliseconds(); KDTimeTask newTask = new KDTimeTask(tid, callback, nowTime + delay, delay, count); bool isRep = false; for (int i = 0; i < taskTimeLst.Count; i++) { if (taskTimeLst[i].tid == tid) { taskTimeLst[i] = newTask; isRep = true; break; } } if (!isRep) { for (int i = 0; i < tmpTimeLst.Count; i++) { if (tmpTimeLst[i].tid == tid) { tmpTimeLst[i] = newTask; isRep = true; break; } } } return(isRep); }
private void DelTimeTask() { if (tmpDelTimeLst.Count > 0) { lock (lockTime) { for (int i = 0; i < tmpDelTimeLst.Count; i++) { bool isDel = false; int delTid = tmpDelTimeLst[i]; for (int j = 0; j < taskTimeLst.Count; j++) { KDTimeTask task = taskTimeLst[j]; if (task.tid == delTid) { isDel = true; taskTimeLst.RemoveAt(j); recTidLst.Add(delTid); //LogInfo("Del taskTimeLst ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()); break; } } if (isDel) { continue; } for (int j = 0; j < tmpTimeLst.Count; j++) { KDTimeTask task = tmpTimeLst[j]; if (task.tid == delTid) { tmpTimeLst.RemoveAt(j); recTidLst.Add(delTid); //LogInfo("Del tmpTimeLst ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()); break; } } } } } }
private void CheckTimeTask() { if (tmpTimeLst.Count > 0) { lock (lockTime) { //加入缓存区中的定时任务 for (int tmpIndex = 0; tmpIndex < tmpTimeLst.Count; tmpIndex++) { taskTimeLst.Add(tmpTimeLst[tmpIndex]); } tmpTimeLst.Clear(); } } //遍历检测任务是否达到条件 nowTime = GetUTCMilliseconds(); for (int index = 0; index < taskTimeLst.Count; index++) { KDTimeTask task = taskTimeLst[index]; if (nowTime.CompareTo(task.destTime) < 0) { continue; } else { Action <int> cb = task.callback; try { if (taskHandle != null) { taskHandle(cb, task.tid); } else { if (cb != null) { cb(task.tid); } } } catch (Exception e) { LogInfo(e.ToString()); } //移除已经完成的任务 if (task.count == 1) { taskTimeLst.RemoveAt(index); index--; recTidLst.Add(task.tid); } else { if (task.count != 0) { task.count -= 1; } task.destTime += task.delay; } } } }