Esempio n. 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
        }
Esempio n. 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
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a <see cref="Task"/> using the <c>Task.FromResult</c> method, but caches the result for the next call.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>Task&lt;T&gt;.</returns>
        public static Task <T> FromResult(T value)
        {
            lock (_fromResultCache)
            {
                if (!_fromResultCache.TryGetValue(value, out var task))
                {
                    task = TaskShim.FromResult(value);
                    _fromResultCache[value] = task;
                }

                return(task);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a <see cref="Task"/> using the <c>Task.FromResult</c> method, but caches the result for the next call.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>Task&lt;T&gt;.</returns>
        public static Task <T> FromResult(T value)
        {
            Task <T> task;

            if (!_fromResultCache.ContainsKey(value))
            {
                task = TaskShim.FromResult(value);
                _fromResultCache[value] = task;
            }
            else
            {
                task = _fromResultCache[value];
            }

            return(task);
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Creates a new async-compatible mutual exclusion lock using the specified wait queue.
 /// </summary>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncLock(IAsyncWaitQueue <IDisposable> queue)
 {
     _queue         = queue;
     _cachedKeyTask = TaskShim.FromResult <IDisposable>(new Key(this));
     _mutex         = new object();
 }