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 = Entity.CreateWithParent <OnceWaitTimer /*, ETTaskCompletionSource<bool>*/>(this, tcs); this.timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); long instanceId = timer.InstanceId; cancellationToken.Register(() => { if (instanceId != timer.InstanceId) { return; } timer.Run(false); this.Remove(timer.Id); }); return(await tcs.Task); }
public long NewOnceTimer(long tillTime, Action action) { OnceTimer timer = Entity.CreateWithParent <OnceTimer /*, Action*/>(this, action); this.timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); return(timer.Id); }
public async ETTask <bool> WaitAsync(long time) { long tillTime = TimeHelper.Now() + time; ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>(); OnceWaitTimer timer = Entity.CreateWithParent <OnceWaitTimer /*, ETTaskCompletionSource<bool>*/>(this, tcs); this.timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); 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 = Entity.CreateWithParent <RepeatedTimer /*, long, Action<bool>*/>(this, time /*, action*/); this.timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); return(timer.Id); }