Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 internal AsyncTimer(AsyncTimerCallback callback, TimeSpan dueTime)
 {
     Task.Delay(dueTime, Token).ContinueWith((t, s) =>
     {
         AsyncTimerCallback cb = (AsyncTimerCallback)s;
         cb();
     }, callback, CancellationToken.None,
         TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion,
         TaskScheduler.Default);
 }
Ejemplo n.º 3
0
 internal AsyncTimer(AsyncTimerCallback callback, TimeSpan dueTime)
 {
     Task.Delay(dueTime, Token).ContinueWith((t, s) =>
     {
         AsyncTimerCallback cb = (AsyncTimerCallback)s;
         cb();
     }, callback, CancellationToken.None,
                                             TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion,
                                             TaskScheduler.Default);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Timer" /> class.
 /// </summary>
 /// <param name="callback">The callback to call with the given period.</param>
 /// <param name="period">The period at which to invoke the callback.</param>
 /// <param name="timerName">Name of the timer.</param>
 /// <param name="loggerFactory">Factory used to create loggers.</param>
 public Timer(
     AsyncTimerCallback callback,
     int period,
     string timerName,
     ILoggerFactory loggerFactory
     )
 {
     this.period    = period;
     this.callback  = callback;
     this.timerName = timerName;
     logger         = loggerFactory.CreateLogger(timerName);
 }
Ejemplo n.º 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] 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)
 {
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 7
0
 /// <inheritdoc />
 public ITimer CreateTimer(AsyncTimerCallback callback, int period, string timerName)
 {
     return(new Timer(callback, period, timerName, loggerFactory));
 }