xtd 0.2.0
Loading...
Searching...
No Matches

◆ elapsed

event<timer, elapsed_event_handler> xtd::timers::timer::elapsed

Occurs when the interval elapses.

Remarks
The xtd::timers::timer::elapsed event is raised if the xtd::timers::timer::enabled property is 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.
If xtd::timers::timer::interval is set after the xtd::timers::timer has started, the count is reset. For example, if you set the interval to 5 seconds and then set xtd::timers::timer::enabled to 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.
If the xtd::timers::timer::synchronizing_object property is std::nullopt,the xtd::timers::timer::elapsed event is raised on a xtd::threading::thread_pool thread. If the processing of the xtd::timers::timer::elapsed event lasts longer than xtd::timers::timer::interval, the event might be raised again on another xtd::threading::thread_pool thread. In this situation, the event handler should be reentrant.
Note
The event-handling method might run on one thread at the same time that another thread calls the xtd::timers::timer::stop method or sets the xtd::timers::timer::enabled property to 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.
Remarks
Even if xtd::timers::timer::synchronizing_object is not std::nullopt, xtd::timers::timer::elapsed events can occur after the xtd::timers::timer::close or xtd::timers::timer::stop method has been called or after the xtd::timers::timer:enabled property has been set to 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.