Time interval management
Class which enables a time interval to be called up by a given function or class by means of a thread.
His code uses the support of platform-independent thread system introduced with C++11.
Example:
#include <kodi/tools/Timer.h>
class ATTR_DLL_LOCAL CExample
{
public:
CExample() : m_timer([this](){TimerCall();})
{
m_timer.Start(5000, true);
}
void TimerCall()
{
fprintf(stderr, "Hello World\n");
}
private:
};
◆ CTimer() [1/2]
Class constructor to pass individual other class as callback.
- Parameters
-
◆ CTimer() [2/2]
CTimer |
( |
std::function< void()> const & | callback | ) |
|
|
inlineexplicit |
Class constructor to pass individual function as callback.
- Parameters
-
[in] | callback | Function to pass as callback about timeout. |
Callback function style:
◆ ~CTimer()
◆ Start()
bool Start |
( |
uint64_t | timeout, |
|
|
bool | interval = false ) |
|
inline |
Start the timer by given time in milliseconds to make his call by arrive of them.
If interval is activated, it calls the associated callback function continuously in the given interval.
- Parameters
-
[in] | timeout | Timeout in milliseconds |
[in] | interval | [opt] To run continuously if true, false only one time and default |
- Returns
- True if successfully done, false if not (callback missing, timeout = 0 or was already running.
◆ Stop()
bool Stop |
( |
bool | wait = false | ) |
|
|
inline |
Stop the timer if it is active.
- Parameters
-
[in] | wait | [opt] Wait until timer is stopped, false is default and call unblocked |
- Returns
- True if timer was active and was stopped, false if already was stopped.
◆ Restart()
Restart timer complete by stop and restart his thread again.
- Note
- Restart only possible as long the timer was running and not done his work.
- Returns
- True if start was successfully done, on error, or if was already finished returned as false
◆ RestartAsync()
void RestartAsync |
( |
uint64_t | timeout | ) |
|
|
inline |
Restart the timer with new timeout without touch of his thread.
- Parameters
-
[in] | timeout | Time as milliseconds to wait for next call |
◆ IsRunning()
Check timer is still active to wait for next call.
- Returns
- True if active, false if all his work done and no more running
◆ GetElapsedSeconds()
float GetElapsedSeconds |
( |
| ) |
const |
|
inline |
Get elapsed time as floating point of timer as seconds.
- Returns
- Elapsed time
◆ GetElapsedMilliseconds()
float GetElapsedMilliseconds |
( |
| ) |
const |
|
inline |
Get elapsed time as floating point of timer as milliseconds.
- Returns
- Elapsed time