private void Run(TimerAction timerAction) { switch (timerAction.TimerClass) { case TimerClass.OnceWaitTimer: { CFTaskCompletionSource <bool> tcs = timerAction.Callback as CFTaskCompletionSource <bool>; this.Remove(timerAction.Id); tcs.SetResult(true); break; } case TimerClass.OnceTimer: { Action action = timerAction.Callback as Action; this.Remove(timerAction.Id); action?.Invoke(); break; } case TimerClass.RepeatedTimer: { Action action = timerAction.Callback as Action; long tillTime = TimeHelper.ServerNow() + timerAction.Time; this.AddTimer(tillTime, timerAction); action?.Invoke(); break; } } }
public async CFTask <bool> WaitTillAsync(long tillTime, CancellationToken cancellationToken = null) { if (TimeHelper.ServerNow() >= tillTime) { return(true); } 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); }
public void Notify(CoroutineLockType coroutineLockType, long key) { CoroutineLockQueueType coroutineLockQueueType = this.list[(int)coroutineLockType]; if (!coroutineLockQueueType.TryGetValue(key, out CoroutineLockQueue queue)) { throw new Exception($"first work notify not find queue"); } if (queue.Count == 0) { coroutineLockQueueType.Remove(key); queue.Dispose(); return; } CFTaskCompletionSource <CoroutineLock> tcs = queue.Dequeue(); tcs.SetResult(EntityFactory.CreateWithParent <CoroutineLock, CoroutineLockType, long>(this, coroutineLockType, key)); }