Timer Class Reference

#include <Pt/System/Timer.h>

Notifies clients in constant intervals. More...

Public Member Functions

 Timer ()
 Default constructor. More...
 
 ~Timer ()
 Destructor. More...
 
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. More...
 
void start (const Pt::Timespan &interval)
 Starts the timer. More...
 
void stop ()
 Stops the timer. More...
 
Signaltimeout ()
 Notifies about interval timeouts. More...
 

Detailed Description

Timers can be used to be notified if a time interval expires. It usually works with an event loop, where the Timer needs to be registered. Timers send the timeout signal in given intervals, to which the interested clients connect. The interval can be changed at any time and timers can switch between an active and inactive state.

The following code calls the function onTimer every second:

void onTimer()
{
std::cerr << "Time out!\n";
}
int main()
{
timer.timeout() += Pt::slot(onTimer);
timer.setActive(loop);
timer.start(1000);
loop.run();
return 0;
}

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.

Signal & timeout()
Notifies about interval timeouts.
Definition: Timer.h:155
void run()
Starts the loop.
Notifies clients in constant intervals.
Definition: Timer.h:73
void setActive(EventLoop &loop)
Sets the used event loop.
EventLoop * loop()
Returns the used event loop.
Definition: Timer.h:95
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: MainLoop.h:67
void start(std::size_t interval)
Starts the timer.