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.
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 }
Contains xtd::event_handler event handler.
void start()
Stops the timer.
Definition: timer.h:71
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
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
void stop()
Starts the timer.
Definition: timer.h:75
event< timer, event_handler > tick
Occurs when the specified timer interval has elapsed and the timer is enabled.
Definition: timer.h:80
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in...
Definition: timer.h:29
Represents an event.
Definition: event.h:21
bool enabled() const
Gets whether the timer is running.
Definition: timer.h:44
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
Contains xtd::event event.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
Contains xtd::forms::component class.
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
The message box contains a symbol consisting of white X in a circle with a red background.
Provides the base implementation and enables object sharing between applications. ...
Definition: component.h:21