Timer Class Reference

#include <Pt/System/Timer.h>

Interval timeout notifications. More...

Public Member Functions

 Timer ()
 Default constructor.
 ~Timer ()
 Destructor.
EventLooploop ()
 Returns the used event loop.
void setActive (EventLoop &loop)
 Sets the used event loop.
void detach ()
 Detach from used event loop.
bool isStarted () const
 Returs true if timer was started.
std::size_t interval () const
 Returns the current timer interval in milliseconds.
void start (std::size_t interval)
 Starts the timer.
void start (const Pt::Timespan &interval)
 Starts the timer.
void stop ()
 Stops the timer.
Signaltimeout ()
 Notifies about interval timeouts.
AsyncWait waitAsync (std::size_t ms)
 Start a one-shot timer delay as a C++20 awaitable.

Detailed Description

Timer emits timeout at a fixed interval. It is not a Selectable. setActive() registers it with an EventLoop. start() begins the interval from the moment it is called. stop() ends it. timeout is not sent until the timer is registered and started.

The interval can be changed while the timer runs. start() again replaces it. detach() removes the timer from the loop. The destructor emits no timeout after that.

void onTimeout()
{
}
timer.timeout() += Pt::slot(onTimeout);
timer.setActive(loop);
timer.start(1000);
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition ConstMethod.h:172
void run()
Starts the loop.
Platform event loop.
Definition MainLoop.h:51
Interval timeout notifications.
Definition Timer.h:74
void setActive(EventLoop &loop)
Sets the used event loop.
EventLoop * loop()
Returns the used event loop.
Definition Timer.h:96
void start(std::size_t interval)
Starts the timer.
Signal & timeout()
Notifies about interval timeouts.
Definition Timer.h:156

Constructor & Destructor Documentation

◆ Timer()

Timer ( )

Constructs an inactive timer.

◆ ~Timer()

~Timer ( )

The destructor sends the destroyed signal.

Member Function Documentation

◆ start() [1/2]

void start ( std::size_t interval)

Start a timer from the moment this method is called. The Timer needs to be registered with an event loop, otherwise the timeout signal will not be sent.

Parameters
intervalTimeout interval in milliseconds

◆ start() [2/2]

void start ( const Pt::Timespan & interval)

Start a timer from the moment this method is called. The Timer needs to be registered with an event loop, otherwise the timeout signal will not be sent.

◆ stop()

void stop ( )

If the Timer is registered with an event loop, the timout signal will not be sent anymore.

◆ timeout()

Signal & timeout ( )

This signal is sent if the interval time has expired.

◆ waitAsync()

AsyncWait waitAsync ( std::size_t ms)

Returns an awaitable that suspends the coroutine until the timer fires after the specified duration. The timer is automatically stopped when the awaitable completes or is cancelled.

The Timer must be registered with an event loop via setActive() before awaiting.

Parameters
msDelay duration in milliseconds.
Returns
Awaitable that suspends until timeout or cancellation.