Click or drag to resize

PolledTimer Class

Implements a timer suitable for use in scenarios that need to poll periodically to see if an action needs to be performed.
Inheritance Hierarchy
SystemObject
  Neon.TimePolledTimer

Namespace:  Neon.Time
Assembly:  Neon.Common (in Neon.Common.dll) Version: 2.2.0
Syntax
public sealed class PolledTimer

The PolledTimer type exposes the following members.

Constructors
  NameDescription
Public methodPolledTimer
The default constructor creates a timer that is initially disabled.
Public methodPolledTimer(Boolean)
The default constructor creates a timer that is initially disabled with optional auto reset capabilities.
Public methodPolledTimer(TimeSpan)
Constructs a timer, initializing it to fire at the specified interval.
Public methodPolledTimer(TimeSpan, Boolean)
Constructs a timer with the option of auto resetting itself.
Top
Properties
  NameDescription
Public propertyFireTime
Returns the scheduled firing time (SYS).
Public propertyHasFired
Determines whether the timer has fired.
Public propertyInterval
The current timer interval.
Top
Methods
  NameDescription
Public methodDisable
Prevents the timer from firing until one of the Reset methods are called or Interval is assigned a new value.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodFireNow
Sets the timer into the fired state.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodReset
Reschedules the timer to fire at the current time plus the timer interval.
Public methodReset(TimeSpan)
Assigns a new interval to the timer and reschedules the timer to fire at the current time plus the new interval.
Public methodResetAddRandom
Reschedules the timer to fire at a random time between the current scheduled firing time and a random interval between Zero and interval.
Public methodResetImmediate
Reschedules the timer to fire immediately.
Public methodResetRandom
Reschedules the timer to fire at a random time between now and the timer interval.
Public methodResetRandomTemporary
Reschedules the timer to fire at the current time plus a randomly selected time between the two intervals passed. This call does not change the underlying timer interval. Subsequent timer resets will continue to use the original interval.
Public methodResetTemporary
Reschedules the timer to fire at the current time plus the specified interval but does not change the underlying timer interval. Subsequent timer resets will continue to use the original interval.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodWaitAsync
Waits aynchronously for the timer to fire.
Top
Remarks

A common programming pattern is to have background threads check periodically for something to do; like resend a message, clean up idle connections, or signal a timeout. The PolledTimer class provides an easy way to encapsulate the interval and next scheduled time at which these events should happen.

Use PolledTimer(TimeSpan) or PolledTimer(TimeSpan, Boolean) to create a timer, specifying the firing interval. The second constructor also specifies the autoReset parameter which indicates that the timer should automatically reschedule itself after being fired. Note that one of these constructors must be used. PolledTimer structures created with the default constructor will thow a InvalidOperationException when an attempt is made to use it.

While being constructed, a polled timer calculates its next scheduled firing time (SYS). Note that this value generated by SysTime not a normal system time. The scheduled firing time is available as the FireTime property. Ther current interval is available as the Interval property.

Use HasFired to determine if the timer has been fired. This will return true if this is the case. If autoReset=true was passed to the constructor, then HasFired will automatically reset the timer by scheduling the next firing time. If autoReset=false, then the timer will remain in the fired state until Reset is called.

Asynchronous applications may find it more convienent to call WaitAsync(TimeSpan) to wait for the timer to fire.

The Reset, ResetImmediate, and ResetRandom methods are used recalcuclate the firing time. The first variation schedules this time as the current time plus the timer interval. The second variation schedules the timer for immediate firing (typically used right after the timer is constructed in situations where the application wishes the timer to fire right away the first time it is polled), and the third method resets the timer to fire at a random interval between zero and the timer's interval (useful when trying to avoid having multiple timers fire at the same time).

The Disable method prevents the timer from firing until Reset is called or Interval is set. This is useful for preventing a timer from firing when an operation initiated from a previous firing is still executing (perhaps on another thread).

Thread Safety
Instance members of this type are safe for multi-threaded operations.
See Also