xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Public Attributes | Public Member Functions | Protected Member Functions | List of all members
xtd::forms::timer Class Referencefinal

#include <timer.h>

Definition

Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.

Namespace
xtd::forms
Library
xtd.forms
Remarks
A timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread.
When you use this timer, use the tick event to perform a polling operation or to display a splash screen for a specified period of time. Whenever the enabled property is set to true and the interval property is greater than zero, the tick event is raised at intervals based on the interval property setting.
This class provides methods to set the interval, and to start and stop the timer.
The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds.
Examples
The following code example demonstrate the use of timer component.
#include <xtd/xtd>
using namespace std::literals;
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::forms;
namespace examples {
class form1 : public form {
public:
form1() {
label1.parent(*this);
label1.text("0.0");
label1.font(drawing::font("Arial", 48, font_style::italic));
label1.location({10, 10});
label1.auto_size(true);
label1.fore_color(color::dodger_blue);
button1.parent(*this);
button1.text("Start");
button1.location({10, 90});
button1.click += [&](object& sender, const event_args& e) {
timer1.enabled(!timer1.enabled());
button1.text(timer1.enabled() ? "Stop": "Start");
};
timer1.interval(100ms);
timer1.tick += [&](object& sender, const event_args& e) {
label1.text(ustring::format("{:F1}", ++counter / 10.0));
};
text("Timer example");
client_size({230, 130});
}
private:
timer timer1;
label label1;
int counter = 0;
};
}
int main() {
application::run(examples::form1());
}
Examples:
dot_matrix_display.cpp, fourteen_segment_display.cpp, lcd_label2.cpp, nine_segment_display.cpp, progress_bar.cpp, seven_segment_display.cpp, sixteen_segment_display.cpp, and timer.cpp.

Public Attributes

event< timer, event_handlertick
 Occurs when the specified timer interval has elapsed and the timer is enabled. More...
 

Public Member Functions

 timer ()=default
 Initializes a new instance of the timer class. More...
 
bool enabled () const
 Gets whether the timer is running. More...
 
void enabled (bool enabled)
 Sets whether the timer is running. More...
 
std::chrono::milliseconds interval () const
 Gets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event. More...
 
template<typename rep_t , typename period_t = std::ratio<1>>
void interval (const std::chrono::duration< rep_t, period_t > &interval)
 Sets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event. More...
 
int32_t interval_milliseconds () const
 Gets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event. More...
 
void interval_milliseconds (int32_t interval)
 Sets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event. More...
 
void start ()
 Stops the timer. More...
 
void stop ()
 Starts the timer. More...
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object. More...
 
virtual 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...
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const
 Gets the type of the current instance. More...
 
virtual xtd::ustring to_string () const noexcept
 Returns a std::string that represents the current object. More...
 

Protected Member Functions

void on_tick (const event_args &e)
 Raises the xtd::forms::timer::tick event. More...
 
- Protected Member Functions inherited from xtd::forms::component
 component ()=default
 Initialises a new instance of the component class. More...
 
virtual bool can_raise_events () const
 Gets a value indicating whether the component can raise an event. More...
 
bool design_mode () const
 Gets a value that indicates whether the component is currently in design mode. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from xtd::object
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...
 

Constructor & Destructor Documentation

◆ timer()

xtd::forms::timer::timer ( )
default

Initializes a new instance of the timer class.

Remarks
When a new timer is created, it is disabled; that is, enabled is set to false. To enable the timer, call the start method or set enabled to true.
If the timer is disabled and the timer is out of scope, the timer will be destroyed.

Member Function Documentation

◆ enabled() [1/2]

bool xtd::forms::timer::enabled ( ) const
inline

Gets whether the timer is running.

Returns
true if the timer is currently enabled; otherwise, false. The default is false.
Remarks
Calling the start method is the same as setting enabled to true. Likewise, calling the stop method is the same as setting enabled to false.
Examples:
lcd_label2.cpp.

◆ enabled() [2/2]

void xtd::forms::timer::enabled ( bool  enabled)

Sets whether the timer is running.

Parameters
enabledtrue if the timer is currently enabled; otherwise, false. The default is false.
Remarks
Calling the start method is the same as setting enabled to true. Likewise, calling the stop method is the same as setting enabled to false.

◆ interval() [1/2]

std::chrono::milliseconds xtd::forms::timer::interval ( ) const
inline

Gets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event.

Returns
An int32 specifying the number of milliseconds before the tick event is raised relative to the last occurrence of the tick event. The value cannot be less than one.
Remarks
To get the number of seconds in the interval, divide this number by 1,000.
Examples:
lcd_label2.cpp.

◆ interval() [2/2]

template<typename rep_t , typename period_t = std::ratio<1>>
void xtd::forms::timer::interval ( const std::chrono::duration< rep_t, period_t > &  interval)
inline

Sets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event.

Parameters
intervalAn int32_t specifying the number of milliseconds before the tick event is raised relative to the last occurrence of the tick event. The value cannot be less than one.
Remarks
To get the number of seconds in the interval, divide this number by 1,000.

◆ interval_milliseconds() [1/2]

int32_t xtd::forms::timer::interval_milliseconds ( ) const
inline

Gets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event.

Returns
An int32 specifying the number of milliseconds before the tick event is raised relative to the last occurrence of the tick event. The value cannot be less than one.
Remarks
To get the number of seconds in the interval, divide this number by 1,000.

◆ interval_milliseconds() [2/2]

void xtd::forms::timer::interval_milliseconds ( int32_t  interval)

Sets the time, in milliseconds, before the tick event is raised relative to the last occurrence of the tick event.

Parameters
intervalAn int32_t specifying the number of milliseconds before the tick event is raised relative to the last occurrence of the tick event. The value cannot be less than one.
Remarks
To get the number of seconds in the interval, divide this number by 1,000.

◆ on_tick()

void xtd::forms::timer::on_tick ( const event_args e)
protected

Raises the xtd::forms::timer::tick event.

Parameters
eAn event_args that contains the event data. This is always xtd::event_args::empty.

◆ start()

void xtd::forms::timer::start ( )
inline

Stops the timer.

Remarks
You can also stop the timer by setting the enabled property to false.

◆ stop()

void xtd::forms::timer::stop ( )
inline

Starts the timer.

Remarks
You can also start the timer by setting the enabled property to true.

The documentation for this class was generated from the following file: