Provides a mechanism for executing a method on a thread pool thread at specified intervals. This class cannot be inherited.
- Inheritance
- xtd::object → xtd::threading::timer
- Header
#include <xtd/threading/timer>
- Namespace
- xtd::threading
- Library
- xtd.core
- Examples
- The following example defines a status_checker class that includes a check_status method whose signature is the same as the xtd::threading::timer_callback delegate. The state argument of the check_status method is an xtd::threading::auto_reset_event object that is used to synchronize the application thread and the thread pool thread that executes the callback delegate. The status_checker class also includes two state variables: invoke_count Indicates the number of times the callback method has been invoked. max_count Determines the maximum number of times the callback method should be invoked. The application thread creates the timer, which waits one second and then executes the check_status callback method every 250 milliseconds. The application thread then blocks until the xtd::threading::auto_reset_event object is signaled. When the check_status callback method executes max_count times, it calls the auto_reset_event::set method to set the state of the xtd::threading::auto_reset_event object to signaled. The first time this happens, the application thread calls the xtd::threading::timer::change(int32, int32) method so that the callback method now executes every half second. It once again blocks until the xtd::threading::auto_reset_event object is signaled.
#include <xtd/threading/auto_reset_event>
#include <xtd/threading/timer>
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
namespace timer_example {
class status_checker {
private:
int invoke_count = 0;
int max_count = 0;
public:
status_checker(int count) {
invoke_count = 0;
max_count = count;
}
void check_status(
std::any state_info) {
auto auto_event = as<auto_reset_event>(state_info);
now, now.millisecond(),
++invoke_count);
if (invoke_count == max_count) {
invoke_count = 0;
auto_event.set();
}
}
};
class program {
public:
static void main() {
auto status_checker = timer_example::status_checker {10};
now, now.millisecond());
auto state_timer = timer {{status_checker, &timer_example::status_checker::check_status},
auto_event, 1000, 250};
auto_event.wait_one();
state_timer.change(0, 500);
auto_event.wait_one();
state_timer.close();
}
};
}
- Note
- xtd includes several timer classes, each of which offers different functionality:
- xtd::timers::timer, which fires an event and executes the code in one or more event sinks at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
- xtd::threading::timer, which executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. Like the xtd::timers::timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
- xtd::forms::timer, a Windows Forms component that fires an event and executes the code in one or more event sinks at regular intervals. The component has no user interface and is designed for use in a single-threaded environment; it executes on the UI thread.
- Note
- As long as you are using a xtd::threading::timer, you must keep a reference to it. As with any managed object, a xtd::threading::timer is subject to garbage collection when there are no references to it. The fact that a xtd::threading::timer is still active does not prevent it from being collecte
-
The system clock that is used is the same clock used by GetTickCount, which is not affected by changes made with timeBeginPeriod and timeEndPeriod.
- Note
- xtd::threading::timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. xtd::forms::timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using xtd::timers::timer, which raises events and has additional features.
- Examples:
- timer.cpp.
|
| timer (const timer_callback &callback) |
| Initializes a new instance of the timer class with an infinite period and an infinite due time, using the newly created timer object as the state object. More...
|
|
| timer (const timer_callback &callback, int32 due_time, int32 period) |
| Initializes a new instance of the timer class, using a 32-bit signed integer to specify the time interval. More...
|
|
| timer (const timer_callback &callback, int64 due_time, int64 period) |
| Initializes a new instance of the timer class, using a 64-bit signed integer to specify the time interval. More...
|
|
| timer (const timer_callback &callback, const time_span &due_time, const time_span &period) |
| Initializes a new instance of the timer class, using a TimaSpan to specify the time interval. More...
|
|
| timer (const timer_callback &callback, uint32 due_time, uint32 period) |
| Initializes a new instance of the timer class, using a 32-bit unsigned integer to specify the time interval. More...
|
|
| timer (const timer_callback &callback, std::any state, int32 due_time, int32 period) |
| Initializes a new instance of the timer class, using a 32-bit signed integer to specify the time interval. More...
|
|
| timer (const timer_callback &callback, std::any state, int64 due_time, int64 period) |
| Initializes a new instance of the timer class, using a 64-bit signed integer to specify the time interval. More...
|
|
| timer (const timer_callback &callback, std::any state, const time_span &due_time, const time_span &period) |
| Initializes a new instance of the timer class, using a TimaSpan to specify the time interval. More...
|
|
| timer (const timer_callback &callback, std::any state, uint32 due_time, uint32 period) |
| Initializes a new instance of the timer class, using a 32-bit unsigned integer to specify the time interval. More...
|
|
|
void | change (int32 due_time, int32 period) |
| changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals. More...
|
|
void | change (int64 due_time, int64 period) |
| changes the start time and the interval between method invocations for a timer, using 64-bit signed integers to measure time intervals. More...
|
|
void | change (const time_span &due_time, const time_span &period) |
| changes the start time and the interval between method invocations for a timer, using time_span values to measure time intervals. More...
|
|
void | change (uint32 due_time, uint32 period) |
| changes the start time and the interval between method invocations for a timer, using 32-bit unsigned integers to measure time intervals. More...
|
|
void | close () |
| Releases all resources used by the current instance of xtd::threading::timer. More...
|
|
|
| object ()=default |
| Create a new instance of the ultimate base class object. More...
|
|
bool | equals (const object &obj) const noexcept |
| Determines whether the specified object is equal to the current object. More...
|
|
virtual size_t | get_hash_code () const noexcept |
| Serves as a hash function for a particular type. More...
|
|
virtual type_object | get_type () const noexcept |
| Gets the type of the current instance. More...
|
|
template<typename object_t > |
std::unique_ptr< object_t > | memberwise_clone () const noexcept |
| Creates a shallow copy of the current object. More...
|
|
virtual xtd::ustring | to_string () const noexcept |
| Returns a sxd::ustring that represents the current object. More...
|
|
static bool | equals (const object &object_a, const object &object_b) noexcept |
| Determines whether the specified object instances are considered equal. More...
|
|
static bool | reference_equals (const object &object_a, const object &object_b) noexcept |
| Determines whether the specified object instances are the same instance. More...
|
|