Class AsyncTimer
Implements a timer that runs on a background Task.
Namespace: Neon.Tasks
Assembly: Neon.Common.dll
Syntax
public class AsyncTimer
Remarks
This class is pretty easy to use. Simply use the AsyncTimer(Func<Task>) constructor to create an instance, passing the async callback to be called when the timer fires and then call Start(TimeSpan, bool, Func<Task>) to start the timer, passing the timer interval.
Start(TimeSpan, bool, Func<Task>) starts a background task that fires the callback at the interval specified. You can call Start(TimeSpan, bool, Func<Task>) again to restart the timer with a different interval. The IsRunning property can be used to determine whether a timer is running or not.
Call Stop() to stop a timer. Start(TimeSpan, bool, Func<Task>) may be called again to restart the timer.
note
This class implements IDisposable so this should be called for every instance created or Stop() should be called explicitly.
note
This class handles any exceptions thrown by the callback by logging them to the default TelemetryHub and then continuing on with firing ticks. You'll need to add a try/catch to your callback to do your own exception handling.
Constructors
AsyncTimer(Func<Task>)
Constructor.
Declaration
public AsyncTimer(Func<Task> callback = null)
Parameters
Type | Name | Description |
---|---|---|
Func<Task> | callback | Optionally specifies the callback. |
Properties
Interval
Returns the timer interval.
Declaration
public TimeSpan Interval { get; }
Property Value
Type | Description |
---|---|
TimeSpan |
IsRunning
Indicates whether the timer is currently running.
note
This returns Zero until Start(TimeSpan, bool, Func<Task>) is called for the first time.
Declaration
public bool IsRunning { get; }
Property Value
Type | Description |
---|---|
bool |
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
Dispose(bool)
Releases any important resources associated with the instance.
Declaration
protected void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | Pass |
~AsyncTimer()
Finalizer.
Declaration
protected ~AsyncTimer()
Start(TimeSpan, bool, Func<Task>)
Starts or restarts the timer.
The interval
must be specified as a positive interval when
the timer is first started but this is optional thereafter, defaulting to
value from the original Start(TimeSpan, bool, Func<Task>) call.
Declaration
public void Start(TimeSpan interval = default, bool delayFirstTick = false, Func<Task> callback = null)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | interval | Optionally specifies the timer interval. |
bool | delayFirstTick | The callback is called immediately by default. You can delay this for
|
Func<Task> | callback | Optionally specifies the timer callback. noteThis must be specified if no callback was passed to the constructor or a previous call to Start(TimeSpan, bool, Func<Task>). |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if this is the first time Start(TimeSpan, bool, Func<Task>) is called for the
instance and |
ObjectDisposedException | Thrown when the instance is disposed. |
Stop()
Stops the timer.
Declaration
public void Stop()
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | Thrown when the instance is disposed. |