public async ETTask <bool> WaitAsync(long time, ETCancellationToken cancellationToken) { long tillTime = TimeHelper.Now() + time; if (TimeHelper.Now() > tillTime) { return(true); } ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>(); OnceWaitTimer timer = TimerFactory.Create <OnceWaitTimer, ETTaskCompletionSource <bool> >(tcs); this.timers[timer.InstanceId] = timer; AddToTimeId(tillTime, timer.InstanceId); long instanceId = timer.InstanceId; cancellationToken.Register(() => { if (instanceId != timer.InstanceId) { return; } timer.Run(false); this.Remove(timer.InstanceId); }); return(await tcs.Task); }
public long NewOnceTimer(long tillTime, Action <bool> action) { OnceTimer timer = TimerFactory.Create <OnceTimer, Action <bool> >(action); this.timers[timer.InstanceId] = timer; AddToTimeId(tillTime, timer.InstanceId); return(timer.InstanceId); }
public async ETTask <bool> WaitAsync(long time) { long tillTime = TimeHelper.Now() + time; ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>(); OnceWaitTimer timer = TimerFactory.Create <OnceWaitTimer, ETTaskCompletionSource <bool> >(tcs); this.timers[timer.InstanceId] = timer; AddToTimeId(tillTime, timer.InstanceId); return(await tcs.Task); }
/// <summary> /// 创建一个RepeatedTimer /// </summary> /// <param name="time"></param> /// <param name="action"></param> /// <returns></returns> public long NewRepeatedTimer(long time, Action <bool> action) { if (time < 30) { throw new Exception($"repeated time < 30"); } long tillTime = TimeHelper.Now() + time; RepeatedTimer timer = TimerFactory.Create <RepeatedTimer, long, Action <bool> >(time, action); this.timers[timer.InstanceId] = timer; AddToTimeId(tillTime, timer.InstanceId); return(timer.InstanceId); }