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