Skip to content

How to create a timer ?

Gammasoft edited this page Nov 16, 2023 · 6 revisions

| xtd | News | Gallery | Examples | Downloads | Documentation | Wiki | Support | Sources | Project | Gammasoft |

There are many method to create a timer with xtd.

Using xtd::threading::timer class instantiation

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.

#include <xtd/diagnostics/stopwatch>
#include <xtd/threading/timer>
#include <xtd/console>

using namespace xtd;
using namespace xtd::diagnostics;
using namespace xtd::threading;

auto main()->int {
  auto stopwatch1 = stopwatch::start_new();
  console::write_line("start at {}", stopwatch1.elapsed());

  auto timer1 = timer([&] {
    console::write_line("do something at {}", stopwatch1.elapsed());
  }, 200_ms, 100_ms);
  
  thread::sleep(1000_ms);
  timer1.close();
  
  console::write_line("stop at {}", stopwatch1.elapsed());
}

Using xtd::timers::timer class instantiation

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.

#include <xtd/diagnostics/stopwatch>
#include <xtd/timers/timer>
#include <xtd/console>

using namespace xtd;
using namespace xtd::diagnostics;
using namespace xtd::threading;
using namespace xtd::timers;

auto main()->int {
  auto stopwatch1 = stopwatch::start_new();
  console::write_line("start at {}", stopwatch1.elapsed());

  auto timer1 = timer(100_ms);
  timer1.elapsed += [&] {
    console::write_line("do something at {}", stopwatch1.elapsed());
  };
  timer1.enabled(true);
  
  thread::sleep(1000_ms);
  timer1.enabled(false);

  console::write_line("stop at {}", stopwatch1.elapsed());
}

Using xtd::forms::timer class instantiation

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.

#include <xtd/diagnostics/stopwatch>
#include <xtd/forms/application>
#include <xtd/forms/debug_form>

using namespace xtd;
using namespace xtd::diagnostics;
using namespace xtd::forms;

auto main()->int {
  auto stop_watch1 = stopwatch::start_new();
  auto debug_form1 = debug_form {};
  auto form1 = form {};

  auto timer1 = timer {};
  timer1.interval(100_ms);
  timer1.tick += [&] {
    debug::write_line("do something at {}", stop_watch1.elapsed());
    if (stop_watch1.elapsed().total_milliseconds() > 1000.0) timer1.enabled(false);
  };
  timer1.enabled(true);

  application::run(form1);
}

Remarks

You can of course use your own timer or any other third-party thread library.

See