Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        /// <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);
        }