示例#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);
        }
示例#2
0
        static PauseToken()
        {
            PauseTokenSource pausedSource = new PauseTokenSource {
                IsPaused = true
            };

            Paused = pausedSource.Token;
        }
示例#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)
 {
 }
示例#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)
 {
 }
示例#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)
 {
 }
示例#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);
        }
示例#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)
 {
 }
示例#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)
 {
 }
示例#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)
 {
 }
示例#10
0
 static PauseToken()
 {
     PauseTokenSource pausedSource = new PauseTokenSource { IsPaused = true };
     Paused = pausedSource.Token;
 }