public void Update() { if (this.TimeId.Count == 0) { return; } long timeNow = TimeHelper.ServerNow(); if (timeNow < this.minTime) { return; } foreach (KeyValuePair <long, List <long> > kv in this.TimeId) { long k = kv.Key; if (k > timeNow) { minTime = k; break; } this.timeOutTime.Enqueue(k); } while (this.timeOutTime.Count > 0) { long time = this.timeOutTime.Dequeue(); foreach (long timerId in this.TimeId[time]) { this.timeOutTimerIds.Enqueue(timerId); } this.TimeId.Remove(time); } while (this.timeOutTimerIds.Count > 0) { long timerId = this.timeOutTimerIds.Dequeue(); TimerAction timerAction = this.GetChild <TimerAction>(timerId); if (timerAction == null) { continue; } Run(timerAction); } }
public bool Remove(long id) { if (id == 0) { return(false); } TimerAction timerAction = this.GetChild <TimerAction>(id); if (timerAction == null) { return(false); } timerAction.Dispose(); return(true); }
public async CFTask <bool> WaitAsync(long time, CancellationToken cancellationToken = null) { if (time == 0) { return(true); } long tillTime = TimeHelper.ServerNow() + time; CFTaskCompletionSource <bool> tcs = new CFTaskCompletionSource <bool>(); TimerAction timer = EntityFactory.CreateWithParent <TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true); this.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (this.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }