Example #1
0
 Token(long scheduledTime, int id, long delay, ITimerTask task)
 {
     ScheduledTime = scheduledTime;
     Id            = id;
     Delay         = delay;
     Task          = task;
 }
Example #2
0
 public HashedWheelTimeout(HashedWheelTimer timer, ITimerTask timerTask, long deadline)
 {
     _timer    = timer;
     TimerTask = timerTask;
     Deadline  = deadline;
     Interlocked.Exchange(ref _state, HashedWheelTimeoutState.Init);
 }
Example #3
0
        public void ScheduleRepeated(ITimerTask task, long period)
        {
            lock (this)
            {
                if (_communicator == null)
                {
                    throw new Ice.CommunicatorDestroyedException();
                }

                var token = new Token(Time.CurrentMonotonicTimeMillis() + period, ++_tokenId, period, task);

                try
                {
                    _tasks.Add(task, token);
                    _tokens.Add(token, null);
                }
                catch (System.ArgumentException)
                {
                    Debug.Assert(false);
                }

                if (token.ScheduledTime < _wakeUpTime)
                {
                    Monitor.Pulse(this);
                }
            }
        }
Example #4
0
        public void Schedule(ITimerTask task, long delay)
        {
            lock (_mutex)
            {
                if (_communicator == null)
                {
                    throw new CommunicatorDestroyedException();
                }

                var token = new Token(Time.CurrentMonotonicTimeMillis() + delay, ++_tokenId, 0, task);

                try
                {
                    _tasks.Add(task, token);
                    _tokens.Add(token, null);
                }
                catch (System.ArgumentException)
                {
                    Debug.Assert(false);
                }

                if (token.ScheduledTime < _wakeUpTime)
                {
                    Monitor.Pulse(_mutex);
                }
            }
        }
Example #5
0
        /// <summary>
        /// 使用实现定时接口ITimerTask的实例创建定时任务
        /// </summary>
        /// <param name="info"></param>
        /// <param name="_ins"></param>
        /// <returns></returns>
        public static Thread CreateTimerTaskService(TimerInfo info, ITimerTask _ins)
        {
            TimerTaskService tus = new TimerTaskService(info, _ins);
            //创建启动线程
            Thread ThreadTimerTaskService = new Thread(new ThreadStart(tus.Start));

            return(ThreadTimerTaskService);
        }
Example #6
0
 public void Dispose()
 {
     (this._task as IDisposable)?.Dispose();
     this.Prev?.Dispose();
     this.Next?.Dispose();
     this._task  = null;
     this._timer = null;
     this.Bucket = null;
     this.Prev   = null;
     this.Next   = null;
 }
Example #7
0
        public ITimeout NewTimeout(ITimerTask task, TimeSpan delay)
        {
            Guard.ArgumentNotNull(task, nameof(task));


            this.Start();
            var now = DateTime.UtcNow.Ticks;
            // 添加任务到下一个时间刻度。
            // 任务被处理后会被装进 HashedWheelBucket 桶。
            long deadline = now + (long)(delay.TotalMilliseconds * 10000L) - startTime;
            HashedWheelTimeout timeout = new HashedWheelTimeout(this, task, deadline);

            _timeouts.Enqueue(timeout);
            return(timeout);
        }
Example #8
0
        public static void RegisterNewAction(ITimerTask task)
        {
            if (timer == null)
            {
                timer       = new DispatcherTimer();
                timer.Tick += (s, e) => Tick();
            }

            if (tasks == null)
            {
                tasks = new List <ITimerTask>();
            }

            tasks.Add(task);
            ReconfigurateTimer();
        }
Example #9
0
        public bool Cancel(ITimerTask task)
        {
            lock (this)
            {
                if (_communicator == null)
                {
                    return(false);
                }

                if (!_tasks.TryGetValue(task, out Token? token))
                {
                    return(false);
                }
                _tasks.Remove(task);
                _tokens.Remove(token);
                return(true);
            }
        }
        /// <summary>
        /// Schedules the specified TimerTask for one-time execution after the specified delay.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="span"></param>
        /// <returns>a handle which is associated with the specified task</returns>
        public ITimeout NewTimeout(ITimerTask task, TimeSpan span)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            if (_cancellationTokenSource != default && _cancellationTokenSource.Token.IsCancellationRequested)
            {
                throw new InvalidOperationException("cannot be started once stopped");
            }

            var pendingTimeoutsCount = _pendingTimeouts.IncrementAndGet();

            if (_maxPendingTimeouts > 0 && pendingTimeoutsCount > _maxPendingTimeouts)
            {
                _pendingTimeouts.DecrementAndGet();
                throw new InvalidOperationException(
                          $"Number of pending timeouts ({pendingTimeoutsCount}) is greater than or equal to maximum allowed pending  timeouts ({_maxPendingTimeouts})");
            }

            Start();

            // Add the timeout to the timeout queue which will be processed on the next tick.
            // During processing all the queued HashedWheelTimeouts will be added to the correct HashedWheelBucket.
            var deadline = DateTimeHelper.TotalMilliseconds + (long)span.TotalMilliseconds - StartTime;

            // Guard against overflow.
            if (span.Milliseconds > 0 && deadline < 0)
            {
                deadline = long.MaxValue;
            }

            var timeout = new HashedWheelTimeout(this, task, deadline);

            _timeouts.Enqueue(timeout);
            return(timeout);
        }
 public VideoInformationHandler(IVideoPlayerHandler videoPlayerHandler, Form1 mainForm)
 {
     _timerTask    = new VideoInformationTimerTask(videoPlayerHandler, mainForm);
     _timerHandler = new TimerHandler(_timerTask);
 }
Example #12
0
 public TimerHandler(ITimerTask timerTask)
 {
     _timerTask = timerTask;
 }
Example #13
0
        private System.Timers.Timer time;                      //定时器

        /// <summary>
        /// 根据定时信息和执行具体任务的实例构造定时任务服务
        /// </summary>
        /// <param name="_timer">定时信息</param>
        /// <param name="_interface">执行具体任务的实例</param>
        private TimerTaskService(TimerInfo _timer, ITimerTask _interface)
        {
            timerInfo         = _timer;
            TimerTaskInstance = _interface;
        }
 public IoThreadBoundTimerTask(INiftyClientChannel channel, ITimerTask timerTask)
 {
     this.channel   = channel;
     this.timerTask = timerTask;
 }
Example #15
0
 public static void scheduleHashedWheel(ITimerTask timerTask, TimeSpan timeSpan)
 {
     HashedWheelTimer.NewTimeout(timerTask, timeSpan);
 }
Example #16
0
 public static void AnExceptionWasThrownBy(this IInternalLogger logger, ITimerTask task, Exception t)
 {
     logger.Warn($"An exception was thrown by {task.GetType().Name}.", t);
 }
Example #17
0
 public HashedWheelTimeout(HashedWheelTimer timer, ITimerTask task, long deadline)
 {
     this._timer   = timer;
     this._task    = task;
     this.Deadline = deadline;
 }
 public VideoFragmentHandler(IVideoPlayerHandler videoPlayerHandler)
 {
     _videoPlayerHandler = videoPlayerHandler;
     _timerTask          = new VideoTimerTask(_videoPlayerHandler);
     _timerHandler       = new TimerHandler(_timerTask);
 }
 public TimerTaskTests()
 {
     _cut = new TimerTask();
 }