Example #1
0
        /// <summary>
        /// Starts the timer.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        private void Start(CancellationToken cancellationToken)
        {
            if (Interval <= 0)
            {
                // Never start a timer
                return;
            }

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

#if USE_INTERNAL_TIMER
            lock (_lock)
            {
                if (_timer != null)
                {
                    _timer.Change(Interval, Timeout.Infinite);
                }
            }
#else
            var delayTask = TaskShim.Delay(Interval, cancellationToken);
            delayTask.ContinueWith(ContinueTimer, cancellationToken, cancellationToken);
#endif
        }
Example #2
0
        /// <summary>
        /// Changes the specified interval.
        /// </summary>
        /// <param name="dueTime">The due time.</param>
        /// <param name="interval">The interval.</param>
        public void Change(TimeSpan dueTime, TimeSpan interval)
        {
            SetUpTimer(dueTime, interval);

            var cancellationToken = _cancellationTokenSource.Token;

            if (dueTime < TimeSpan.Zero)
            {
                // Never invoke initial one
            }
            else if (dueTime == TimeSpan.Zero)
            {
                // Invoke immediately
                TimerElapsed();

                Start(cancellationToken);
            }
            else
            {
                // Invoke after due time
#if USE_INTERNAL_TIMER
                lock (_lock)
                {
                    if (_timer != null)
                    {
                        _timer.Change(dueTime, Timeout.InfiniteTimeSpan);
                    }
                }
#else
                var delayTask = TaskShim.Delay(dueTime, cancellationToken);
                delayTask.ContinueWith(ContinueTimer, cancellationToken, cancellationToken);
#endif
            }
        }
Example #3
0
        /// <summary>
        /// Changes the specified interval.
        /// </summary>
        /// <param name="dueTime">The due time.</param>
        /// <param name="interval">The interval.</param>
        public void Change(TimeSpan dueTime, TimeSpan interval)
        {
            SetUpTimer(dueTime, interval);

            var cancellationToken = GetCancellationToken();

            if (dueTime < TimeSpan.Zero)
            {
                // Never invoke initial one
            }
            else if (dueTime == TimeSpan.Zero)
            {
                // Invoke immediately
                TimerElapsed();

                Start(cancellationToken);
            }
            else
            {
                // Invoke after due time
#if USE_INTERNAL_TIMER
                lock (_lock)
                {
                    if (_timer != null)
                    {
                        _timer.Change(dueTime, Timeout.InfiniteTimeSpan);
                    }
                }
#else
                var delayTask = TaskShim.Delay(dueTime, cancellationToken);
                delayTask.ContinueWith(ContinueTimer, cancellationToken, cancellationToken);
#endif
            }

            // Note: don't use SafeInvoke, this event could be raised a *lot*
            var changed = Changed;
            if (changed != null)
            {
                changed(this, EventArgs.Empty);
            }
        }