Generates an event after a set interval, with an option to generate recurring events.
false
, a xtd::timers::timer object raises the xtd::timers::timer::elapsed event only once, after the first xtd::timers::timer::iInterval has elapsed. To keep raising the xtd::timers::timer::elapsed event regularly at the interval defined by the xtd::timers::timer::interval, set xtd::timers::timer::auto_reset to true
, which is the default value. false
. This might result in the xtd::timers::timer::elapsed event being raised after the timer is stopped. The example code for the xtd::timers::timerstop method shows one way to avoid this race condition. false
, because the signal to raise the xtd::timers::timer::elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the xtd::timers::timer::elapsed event to ignore subsequent events. Public Events | |
event< timer, elapsed_event_handler > | elapsed |
Occurs when the interval elapses. | |
Public Constructors | |
timer () | |
Initializes a new instance of the xtd::timers::timer class, and sets all the properties to their initial values. | |
timer (double interval) | |
Initializes a new instance of the xtd::timers::timer class, and sets the xtd::timers::timer::interval property to the specified number of milliseconds. | |
timer (const xtd::time_span &interval) | |
Initializes a new instance of the xtd::timers::timer class, setting the xtd::timers::timer::interval property to the specified period. | |
Public Properties | |
bool | auto_reset () const noexcept |
Gets a boolean indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event only once (false) or repeatedly (true). | |
timer & | auto_reset (bool value) |
Sets a boolean indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event only once (false) or repeatedly (true). | |
bool | enabled () const noexcept |
Gets a value indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event. | |
timer & | enabled (bool value) |
Sets a value indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event. | |
double | interval () const noexcept |
Gets the interval, expressed in milliseconds, at which to raise the xtd::timers::timer::elapsed event. | |
timer & | interval (double value) |
Sets the interval, expressed in milliseconds, at which to raise the xtd::timers::timer::elapsed event. | |
std::optional< std::reference_wrapper< isynchronize_invoke > > | synchronizing_object () const noexcept |
Gets the object used to marshal event-handler calls that are issued when an interval has elapsed. | |
timer & | synchronizing_object (isynchronize_invoke &value) |
Sets the object used to marshal event-handler calls that are issued when an interval has elapsed. | |
timer & | synchronizing_object (std::nullptr_t value) |
Resets the object used to marshal event-handler calls that are issued when an interval has elapsed. | |
Public Methods | |
void | close () |
Releases the resources used by the xtd::timers::timer. | |
void | start () |
Starts raising the xtd::timers::timer::elapsed event by setting xtd::timers::timer::enabled to true . | |
void | stop () |
Stops raising the xtd::timers::timer::elapsed event by setting xtd::timers::timer::enabled to false . | |
Additional Inherited Members | |
![]() | |
object ()=default | |
Create a new instance of the ultimate base class object. | |
virtual bool | equals (const object &obj) const noexcept |
Determines whether the specified object is equal to the current object. | |
virtual xtd::size | get_hash_code () const noexcept |
Serves as a hash function for a particular type. | |
virtual type_object | get_type () const noexcept |
Gets the type of the current instance. | |
template<class object_t > | |
xtd::uptr< object_t > | memberwise_clone () const |
Creates a shallow copy of the current object. | |
virtual xtd::string | to_string () const noexcept |
Returns a xtd::string that represents the current object. | |
![]() | |
template<class object_a_t , class object_b_t > | |
static bool | equals (const object_a_t &object_a, const object_b_t &object_b) noexcept |
Determines whether the specified object instances are considered equal. | |
template<class object_a_t , class object_b_t > | |
static bool | reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept |
Determines whether the specified object instances are the same instance. | |
xtd::timers::timer::timer | ( | ) |
Initializes a new instance of the xtd::timers::timer class, and sets all the properties to their initial values.
Property | Initial alue |
---|---|
xtd::timers::timer::auto_reset | true |
xtd::timers::timer::enabled | false |
xtd::timers::timer::nterval | 100 milliseconds |
xtd::timers::timer::synchronizing_object | std::nullopt |
|
explicit |
Initializes a new instance of the xtd::timers::timer class, and sets the xtd::timers::timer::interval property to the specified number of milliseconds.
interval | The time, in milliseconds, between events. The value must be greater than zero and less than or equal to int32_object::max_value. |
d::argument_exception | The value of the interval parameter is less than or equal to zero, or greater than int32_object::max_value. |
|
explicit |
Initializes a new instance of the xtd::timers::timer class, setting the xtd::timers::timer::interval property to the specified period.
interval | The time between events. The value in milliseconds must be greater than zero and less than or equal to int32_object::max_value. |
|
noexcept |
Gets a boolean indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event only once (false) or repeatedly (true).
true
if the xtd::timers::timer should raise the xtd::timers::timer::elapsed event each time the interval elapses; false
if it should raise the xtd::timers::timer::elapsed event only once, after the first time the interval elapses. The default is true
. false
, the xtd::timers::timer::start method must be called in order to start the count again. true
, the count starts at the time xtd::timers::timer::enabled is set. If you reset the interval to 10 seconds when the count is 3 seconds, the xtd::timers::timer::elapsed event is raised for the first time 13 seconds after the xtd::timers::timer::enabled property was set to true
. timer & xtd::timers::timer::auto_reset | ( | bool | value | ) |
Sets a boolean indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event only once (false) or repeatedly (true).
value | true if the xtd::timers::timer should raise the xtd::timers::timer::elapsed event each time the interval elapses; false if it should raise the xtd::timers::timer::elapsed event only once, after the first time the interval elapses. The default is true . |
false
, the xtd::timers::timer::start method must be called in order to start the count again. true
, the count starts at the time xtd::timers::timer::enabled is set. If you reset the interval to 10 seconds when the count is 3 seconds, the xtd::timers::timer::elapsed event is raised for the first time 13 seconds after the xtd::timers::timer::enabled property was set to true
.
|
noexcept |
Gets a value indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event.
true
if the xtd::timers::timer should raise the xtd::timers::timer::elapsed event; otherwise, false
. The default is false
. xtd::object_closed_exception | This property cannot be set because the timer has been closed. |
xtd::argument_exception | The xtd::timers::timer::interval property was set to a value greater than xtd::int32_object::max_value before the timer was enabled. |
true
is the same as calling xtd::timers::timer::start, while setting xtd::timers::timer::enabled to false
is the same as calling xtd::timers::timer::stop. false
. The code example for the xtd::timers::timer::stop method shows one way to work around this race condition. true
and xtd::timers::timer::auto_reset is set to false
, the xtd::timers::timer raises the xtd::timers::timer::elapsed event only once, the first time the interval elapses. true
, the count starts at the time xtd::timers::timer::enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the xtd::timers::timer::elapsed event is raised for the first time 13 seconds after Enabled was set to true
. timer & xtd::timers::timer::enabled | ( | bool | value | ) |
Sets a value indicating whether the xtd::timers::timer should raise the xtd::timers::timer::elapsed event.
value | true if the xtd::timers::timer should raise the xtd::timers::timer::elapsed event; otherwise, false . The default is false . |
xtd::object_closed_exception | This property cannot be set because the timer has been closed. |
xtd::argument_exception | The xtd::timers::timer::interval property was set to a value greater than xtd::int32_object::max_value before the timer was enabled. |
true
is the same as calling xtd::timers::timer::start, while setting xtd::timers::timer::enabled to false
is the same as calling xtd::timers::timer::stop. false
. The code example for the xtd::timers::timer::stop method shows one way to work around this race condition. true
and xtd::timers::timer::auto_reset is set to false
, the xtd::timers::timer raises the xtd::timers::timer::elapsed event only once, the first time the interval elapses. true
, the count starts at the time xtd::timers::timer::enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the xtd::timers::timer::elapsed event is raised for the first time 13 seconds after Enabled was set to true
.
|
noexcept |
Gets the interval, expressed in milliseconds, at which to raise the xtd::timers::timer::elapsed event.
xtd::argument_exception | The interval is less than or equal to zero. -or The interval is greater than xtd::int32_object::max_value. |
true
. true
and xtd::timers::timer::auto_reset is set to false
, the xtd::timers::timer raises the xtd::timers::timer::elapsed event only once, the first time the interval elapses. xtd::timers::timer::enabled is then set to false
. false
, and the timer has previously been enabled, setting the xtd::timers::timer::interval property causes the xtd::timers::timer::elapsed event to be raised once, as if the xtd::timers::timer::enabled property had been set to true
. To set the interval without raising the event, you can temporarily set the xtd::timers::timer::enabled property to true
, set the xtd::timers::timer::interval property to the desired time interval, and then immediately set the xtd::timers::timer::enabled property back to false
. timer & xtd::timers::timer::interval | ( | double | value | ) |
Sets the interval, expressed in milliseconds, at which to raise the xtd::timers::timer::elapsed event.
value | The time, in milliseconds, between xtd::timers::timer::elapsed events. The value must be greater than zero, and less than or equal to xtd::int32_object::max_value. The default is 100 milliseconds. |
xtd::argument_exception | The interval is less than or equal to zero. -or The interval is greater than xtd::int32_object::max_value. |
true
. true
and xtd::timers::timer::auto_reset is set to false
, the xtd::timers::timer raises the xtd::timers::timer::elapsed event only once, the first time the interval elapses. xtd::timers::timer::enabled is then set to false
. false
, and the timer has previously been enabled, setting the xtd::timers::timer::interval property causes the xtd::timers::timer::elapsed event to be raised once, as if the xtd::timers::timer::enabled property had been set to true
. To set the interval without raising the event, you can temporarily set the xtd::timers::timer::enabled property to true
, set the xtd::timers::timer::interval property to the desired time interval, and then immediately set the xtd::timers::timer::enabled property back to false
.
|
noexcept |
Gets the object used to marshal event-handler calls that are issued when an interval has elapsed.
false
, because the signal to raise the xtd::timers::timer::elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the xtd::timers::timer::elapsed event to ignore subsequent events. timer & xtd::timers::timer::synchronizing_object | ( | isynchronize_invoke & | value | ) |
Sets the object used to marshal event-handler calls that are issued when an interval has elapsed.
value | The xtd::isynchronize_invoke representing the object used to marshal the event-handler calls that are issued when an interval has elapsed. The default is std::nullopt. |
false
, because the signal to raise the xtd::timers::timer::elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the xtd::timers::timer::elapsed event to ignore subsequent events. The following example is a Windows Forms app that serves as a very simple text file editor. When the text in the text box has not been saved, the app asks the user at one-minute intervals whether they want to save the contents of the text box. To do this, the xtd::timers::timer::interval property is set to one minute (60,000 milliseconds), and the xtd::timers::timer::synchronizing_object property is set to the xtd::forms::form object. timer & xtd::timers::timer::synchronizing_object | ( | std::nullptr_t | value | ) |
Resets the object used to marshal event-handler calls that are issued when an interval has elapsed.
value | nullptr. |
false
, because the signal to raise the xtd::timers::timer::elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the xtd::timers::timer::elapsed event to ignore subsequent events. The following example is a Windows Forms app that serves as a very simple text file editor. When the text in the text box has not been saved, the app asks the user at one-minute intervals whether they want to save the contents of the text box. To do this, the xtd::timers::timer::interval property is set to one minute (60,000 milliseconds), and the xtd::timers::timer::synchronizing_object property is set to the xtd::forms::form object. void xtd::timers::timer::close | ( | ) |
Releases the resources used by the xtd::timers::timer.
void xtd::timers::timer::start | ( | ) |
Starts raising the xtd::timers::timer::elapsed event by setting xtd::timers::timer::enabled to true
.
false
, the xtd::timers::timer raises the xtd::timers::timer::elapsed event only once, the first time the interval elapses. If xtd::timers::timer::start is called and xtd::timers::timer::auto_reset is true
, the xtd::timers::timer raises the xtd::timers::timer::elapsed event the first time the interval elapses and continues to raise the event on the specified interval. false
, the xtd::timers::timer::start method must be called in order to start the count again. void xtd::timers::timer::stop | ( | ) |
Stops raising the xtd::timers::timer::elapsed event by setting xtd::timers::timer::enabled to false
.
false
. event<timer, elapsed_event_handler> xtd::timers::timer::elapsed |
Occurs when the interval elapses.
true
and the time interval (in milliseconds) defined by the xtd::timers::timer::interval property elapses. If the xtd::timers::timer::auto_reset property is true
, the event is raised repeatedly at an interval defined by the xtd::timers::timer::interval property; otherwise, the event is raised only once, the first time the xtd::timers::timer::interval value elapses. true
, the count starts at the time xtd::timers::timer::enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the xtd::timers::timer::elapsed event is raised for the first time 13 seconds after xtd::timers::timer::enabled was set to true
. false
. This might result in the xtd::timers::timer::elapsed event being raised after the timer is stopped. The example code for the Stop method shows one way to avoid this race condition. false
, because the signal to raise the xtd::timers::timer::elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the xtd::timers::timer::elapsed event to ignore subsequent events.