Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
        /// </summary>
        /// <param name="callback">The asynchronous method to be executed.</param>
        /// <param name="period">The minimum gap between the start of the task invocation and the start of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="TimeHelpers.InfiniteDuration" />).</param>
        /// <param name="dueTime">The due time between the last time the timeouts were changed and the start of the task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
        /// <param name="minimumGap">The minimum gap between the start of the task invocation and the end of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
        /// <param name="pauseToken">The pause token for pasuing the timer.</param>
        /// <param name="errorHandler">The optional error handler.</param>
        public AsyncTimer(
            [NotNull] AsyncTimerCallback callback,
            Duration?period                 = null,
            Duration?dueTime                = null,
            Duration?minimumGap             = null,
            PauseToken pauseToken           = default(PauseToken),
            Action <Exception> errorHandler = null)
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            long timeStamp = HighPrecisionClock.Instance.NowTicks;

            _callback   = callback;
            _pauseToken = pauseToken;
            _timeOuts   = new TimeOuts(
                period ?? TimeHelpers.InfiniteDuration,
                dueTime ?? Duration.Zero,
                minimumGap ?? Duration.Zero,
                timeStamp);

            _cancellationTokenSource  = new CancellationTokenSource();
            _timeOutsChanged          = new CancellationTokenSource();
            _callbackCompletionSource = null;

            _errorHandler = errorHandler;

            Task.Run(() => TimerTask(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
        }
Example #2
0
        static PauseToken()
        {
            PauseTokenSource pausedSource = new PauseTokenSource {
                IsPaused = true
            };

            Paused = pausedSource.Token;
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
 /// </summary>
 /// <param name="callback">The callback.</param>
 /// <param name="period">The minimum gap (in milliseconds) between the start of the task invocation and the start of the previous task invocation (defaults to <see cref="Timeout.Infinite"/>).</param>
 /// <param name="dueTime">The due time (in milliseconds) between the last time the timeouts were changed and the start of the task invocation (defaults to 0ms).</param>
 /// <param name="minimumGap">The minimum gap (in milliseconds) between the start of the task invocation and the end of the previous task invocation (defaults to 0ms).</param>
 /// <param name="pauseToken">The pause token for pasuing the timer.</param>
 /// <param name="errorHandler">The optional error handler.</param>
 public AsyncTimer(
     [NotNull] AsyncTimerCallback callback,
     int period                      = Timeout.Infinite,
     int dueTime                     = 0,
     int minimumGap                  = 0,
     PauseToken pauseToken           = default(PauseToken),
     Action <Exception> errorHandler = null)
     : this(callback,
            Duration.FromMilliseconds(period),
            Duration.FromMilliseconds(dueTime),
            Duration.FromMilliseconds(minimumGap),
            pauseToken,
            errorHandler)
 {
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
 /// </summary>
 /// <param name="callback">The asynchronous method to be executed.</param>
 /// <param name="period">The minimum gap between the start of the task invocation and the start of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="TimeHelpers.InfiniteDuration" />).</param>
 /// <param name="dueTime">The due time between the last time the timeouts were changed and the start of the task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
 /// <param name="minimumGap">The minimum gap between the start of the task invocation and the end of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
 /// <param name="pauseToken">The pause token for pasuing the timer.</param>
 /// <param name="errorHandler">The optional error handler.</param>
 public AsyncTimer(
     [NotNull] Action <CancellationToken> callback,
     Duration?period                 = null,
     Duration?dueTime                = null,
     Duration?minimumGap             = null,
     PauseToken pauseToken           = default(PauseToken),
     Action <Exception> errorHandler = null)
     : this(
         t =>
 {
     callback(t);
     return(TaskResult.Completed);
 },
         period,
         dueTime,
         minimumGap,
         pauseToken,
         errorHandler)
 {
 }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
 /// </summary>
 /// <param name="callback">The callback.</param>
 /// <param name="period">The minimum gap (in milliseconds) between the start of the task invocation and the start of the previous task invocation (defaults to <see cref="Timeout.Infinite"/>).</param>
 /// <param name="dueTime">The due time (in milliseconds) between the last time the timeouts were changed and the start of the task invocation (defaults to 0ms).</param>
 /// <param name="minimumGap">The minimum gap (in milliseconds) between the start of the task invocation and the end of the previous task invocation (defaults to 0ms).</param>
 /// <param name="pauseToken">The pause token for pasuing the timer.</param>
 /// <param name="errorHandler">The optional error handler.</param>
 public AsyncTimer(
     [NotNull] Action <CancellationToken> callback,
     int period                      = Timeout.Infinite,
     int dueTime                     = 0,
     int minimumGap                  = 0,
     PauseToken pauseToken           = default(PauseToken),
     Action <Exception> errorHandler = null)
     : this(
         t =>
 {
     callback(t);
     return(TaskResult.Completed);
 },
         Duration.FromMilliseconds(period),
         Duration.FromMilliseconds(dueTime),
         Duration.FromMilliseconds(minimumGap),
         pauseToken,
         errorHandler)
 {
 }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
        /// </summary>
        /// <param name="callback">The asynchronous method to be executed.</param>
        /// <param name="period">The minimum gap between the start of the task invocation and the start of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="TimeHelpers.InfiniteDuration" />).</param>
        /// <param name="dueTime">The due time between the last time the timeouts were changed and the start of the task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
        /// <param name="minimumGap">The minimum gap between the start of the task invocation and the end of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
        /// <param name="pauseToken">The pause token for pasuing the timer.</param>
        /// <param name="errorHandler">The optional error handler.</param>
        public AsyncTimer(
            [NotNull] AsyncTimerCallback callback,
            Duration? period = null,
            Duration? dueTime = null,
            Duration? minimumGap = null,
            PauseToken pauseToken = default(PauseToken),
            Action<Exception> errorHandler = null)
        {
            if (callback == null) throw new ArgumentNullException(nameof(callback));

            long timeStamp = HighPrecisionClock.Instance.NowTicks;
            _callback = callback;
            _pauseToken = pauseToken;
            _timeOuts = new TimeOuts(
                period ?? TimeHelpers.InfiniteDuration,
                dueTime ?? Duration.Zero,
                minimumGap ?? Duration.Zero,
                timeStamp);

            _cancellationTokenSource = new CancellationTokenSource();
            _timeOutsChanged = new CancellationTokenSource();
            _callbackCompletionSource = null;

            _errorHandler = errorHandler;

            Task.Run(() => TimerTask(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
 /// </summary>
 /// <param name="callback">The callback.</param>
 /// <param name="period">The minimum gap (in milliseconds) between the start of the task invocation and the start of the previous task invocation (defaults to <see cref="Timeout.Infinite"/>).</param>
 /// <param name="dueTime">The due time (in milliseconds) between the last time the timeouts were changed and the start of the task invocation (defaults to 0ms).</param>
 /// <param name="minimumGap">The minimum gap (in milliseconds) between the start of the task invocation and the end of the previous task invocation (defaults to 0ms).</param>
 /// <param name="pauseToken">The pause token for pasuing the timer.</param>
 /// <param name="errorHandler">The optional error handler.</param>
 public AsyncTimer(
     [NotNull] AsyncTimerCallback callback,
     int period = Timeout.Infinite,
     int dueTime = 0,
     int minimumGap = 0,
     PauseToken pauseToken = default(PauseToken),
     Action<Exception> errorHandler = null)
     : this(callback,
            Duration.FromMilliseconds(period),
            Duration.FromMilliseconds(dueTime),
            Duration.FromMilliseconds(minimumGap),
            pauseToken,
            errorHandler)
 {
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
 /// </summary>
 /// <param name="callback">The asynchronous method to be executed.</param>
 /// <param name="period">The minimum gap between the start of the task invocation and the start of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="TimeHelpers.InfiniteDuration" />).</param>
 /// <param name="dueTime">The due time between the last time the timeouts were changed and the start of the task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
 /// <param name="minimumGap">The minimum gap between the start of the task invocation and the end of the previous task invocation (defautls to <see langword="null" /> which is equivalent to <see cref="Duration.Zero" />).</param>
 /// <param name="pauseToken">The pause token for pasuing the timer.</param>
 /// <param name="errorHandler">The optional error handler.</param>
 public AsyncTimer(
     [NotNull] Action<CancellationToken> callback,
     Duration? period = null,
     Duration? dueTime = null,
     Duration? minimumGap = null,
     PauseToken pauseToken = default(PauseToken),
     Action<Exception> errorHandler = null)
     : this(
         t =>
         {
             callback(t);
             return TaskResult.Completed;
         },
         period,
         dueTime,
         minimumGap,
         pauseToken,
         errorHandler)
 {
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncTimer" /> class.
 /// </summary>
 /// <param name="callback">The callback.</param>
 /// <param name="period">The minimum gap (in milliseconds) between the start of the task invocation and the start of the previous task invocation (defaults to <see cref="Timeout.Infinite"/>).</param>
 /// <param name="dueTime">The due time (in milliseconds) between the last time the timeouts were changed and the start of the task invocation (defaults to 0ms).</param>
 /// <param name="minimumGap">The minimum gap (in milliseconds) between the start of the task invocation and the end of the previous task invocation (defaults to 0ms).</param>
 /// <param name="pauseToken">The pause token for pasuing the timer.</param>
 /// <param name="errorHandler">The optional error handler.</param>
 public AsyncTimer(
     [NotNull] Action<CancellationToken> callback,
     int period = Timeout.Infinite,
     int dueTime = 0,
     int minimumGap = 0,
     PauseToken pauseToken = default(PauseToken),
     Action<Exception> errorHandler = null)
     : this(
         t =>
         {
             callback(t);
             return TaskResult.Completed;
         },
         Duration.FromMilliseconds(period),
         Duration.FromMilliseconds(dueTime),
         Duration.FromMilliseconds(minimumGap),
         pauseToken,
         errorHandler)
 {
 }
Example #10
0
 static PauseToken()
 {
     PauseTokenSource pausedSource = new PauseTokenSource { IsPaused = true };
     Paused = pausedSource.Token;
 }