xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
timer.h
Go to the documentation of this file.
1 #pragma once
5 #include <cstdint>
6 #include <chrono>
7 #include <memory>
8 #include <xtd/event.h>
9 #include <xtd/event_handler.h>
10 #include "component.h"
11 
13 namespace xtd {
15  namespace forms {
29  class forms_export_ timer final : public component {
30  public:
34  timer() = default;
35 
37  timer(const timer&) = delete;
38  ~timer() {stop();}
40 
44  bool enabled() const {return enabled_;}
48  void enabled(bool enabled);
49 
53  std::chrono::milliseconds interval() const {return std::chrono::milliseconds(interval_);}
57  template<typename rep_t, typename period_t = std::ratio<1>>
58  void interval(const std::chrono::duration<rep_t, period_t>& interval) {interval_milliseconds(static_cast<int32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(interval).count()));}
59 
63  int32_t interval_milliseconds() const {return interval_;}
67  void interval_milliseconds(int32_t interval);
68 
71  void start() {enabled(true);}
72 
75  void stop() {enabled(false);}
76 
81 
82  protected:
85  void on_tick(const event_args& e);
86 
87  private:
88  bool enabled_ = false;
89  int32_t interval_ = 100;
90  intptr_t handle_ = 0;
91  };
92  }
93 }
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
Represents an event.
Definition: event.h:21
Provides the base implementation and enables object sharing between applications.
Definition: component.h:21
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in...
Definition: timer.h:29
std::chrono::milliseconds interval() const
Gets the time, in milliseconds, before the tick event is raised relative to the last occurrence of th...
Definition: timer.h:53
void stop()
Starts the timer.
Definition: timer.h:75
void on_tick(const event_args &e)
Raises the xtd::forms::timer::tick event.
bool enabled() const
Gets whether the timer is running.
Definition: timer.h:44
int32_t interval_milliseconds() const
Gets the time, in milliseconds, before the tick event is raised relative to the last occurrence of th...
Definition: timer.h:63
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 th...
Definition: timer.h:58
void interval_milliseconds(int32_t interval)
Sets the time, in milliseconds, before the tick event is raised relative to the last occurrence of th...
void enabled(bool enabled)
Sets whether the timer is running.
void start()
Stops the timer.
Definition: timer.h:71
timer()=default
Initializes a new instance of the timer class.
Contains xtd::forms::component class.
Contains xtd::event event.
Contains xtd::event_handler event handler.
event< timer, event_handler > tick
Occurs when the specified timer interval has elapsed and the timer is enabled.
Definition: timer.h:80
#define forms_export_
Define shared library export.
Definition: forms_export.h:13
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