xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
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());
}
Defines a particular format for text, including font face, size, and style attributes....
Definition: font.h:39
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
static void run()
Begins running a standard application message loop on the current thread, without a form.
Represents a Windows button control.
Definition: button.h:54
virtual const xtd::ustring & text() const
Gets the text associated with this control.
Definition: control.h:650
Represents a window or dialog box that makes up an application's user interface.
Definition: form.h:40
control & parent(const control &value) override
Sets the parent container of the control.
Represents a standard Windows label.
Definition: label.h:26
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in...
Definition: timer.h:29
@ e
The E key.
@ button1
The first button on the message box is the default button.
The xtd::drawing namespace provides access to GDI+ basic graphics functionality. More advanced functi...
Definition: bitmap.h:11
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
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.

Inherits xtd::forms::component.

Public Attributes

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

Public Member Functions

 timer ()=default
 Initializes a new instance of the timer class.
 
bool enabled () const
 Gets whether the timer is running.
 
void enabled (bool enabled)
 Sets whether the timer is running.
 
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.
 
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.
 
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.
 
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.
 
void start ()
 Stops the timer.
 
void stop ()
 Starts the timer.
 
- Public Member Functions inherited from xtd::object
 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 size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const
 Gets the type of the current instance.
 
virtual xtd::ustring to_string () const noexcept
 Returns a std::string that represents the current object.
 

Protected Member Functions

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

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.
 
static bool reference_equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

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: