Advanced Timer
The AdvancedTimer
class is a custom timer implementation that extends the functionality of the System.Timers.Timer
class. It provides additional features for tracking and managing elapsed time intervals, making it useful for various timing and scheduling scenarios in .NET 6.0 and later.
Namespace
License
The AdvancedTimer
class is part of the Chase CommonLib library, developed by LFInteractive LLC, and is licensed under the GNU General Public License (GPL-3.0).
Constructor
AdvancedTimer(double duration)
AdvancedTimer(double duration)
Creates an instance of the AdvancedTimer
class with a specified duration in milliseconds.
Parameters:
duration
(double): The duration of the timer in milliseconds.
Example:
Properties
Enabled
Enabled
Gets or sets a value indicating whether the AdvancedTimer
is able to raise events at a defined interval. This property overrides the base Enabled
property from System.Timers.Timer
.
Type: bool
Read-only
Interruptible
Interruptible
Gets or sets whether the Start()
method can be run while the timer is already running. When set to true
, the timer will restart without raising the event. When set to false
, an exception will be thrown if Start()
is called while the timer is running.
Type: bool
Read-write
IsRunning
IsRunning
Indicates whether the AdvancedTimer
is currently running or stopped. This property is set when using the Start()
and Stop()
methods.
Type: bool
Read-only
Methods
GetNextElapseTime()
GetNextElapseTime()
Gets the time when the timer will elapse next.
Returns: DateTime
Example:
GetRemaining()
GetRemaining()
Gets the number of milliseconds remaining until the next timer elapse is triggered.
Returns: double
Example:
GetRemainingTime()
GetRemainingTime()
Gets the time remaining until the next timer elapse is triggered as a TimeSpan
.
Returns: TimeSpan
Example:
Reset()
Reset()
Restarts the AdvancedTimer
without triggering the elapse event.
Example:
Start()
Start()
Starts the AdvancedTimer
. If called while the timer is already running and Interruptible
is set to false
, an exception is thrown.
Exceptions:
InvalidOperationException
- Thrown when the timer is already running, andInterruptible
is set tofalse
.
Example:
Stop()
Stop()
Stops the AdvancedTimer
.
Example:
ToString()
ToString()
Overrides the ToString()
method to provide a custom representation of the AdvancedTimer
object. If the timer is running, it returns the remaining time as a string; otherwise, it returns a JSON representation of the object.
Returns: string
Example:
Example Usage
In this example, an AdvancedTimer
is created with a 2-second interval. It is started, and its properties and methods are demonstrated, including checking the remaining time until the next elapse, stopping the timer, resetting it, and getting the next elapse time.
Last updated
Was this helpful?