-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.cpp
41 lines (33 loc) · 1.38 KB
/
Timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "Timer.h"
void Timer::runTimer(std::chrono::milliseconds delay, std::function<void()> callback)
{
currentTimerThread_ = std::thread([&, delay]
{
std::chrono::milliseconds counter = std::chrono::milliseconds(0);
while (counter < delay && !isInterrupted_)
{
std::this_thread::sleep_for(updateStep_);
counter += updateStep_;
}
if (!isInterrupted_)
{
callback();
}
else
{
timeRemains_ = delay - counter;
}
currentTimerThread_.detach();
});
}
std::chrono::milliseconds Timer::stopTimer()
{
isInterrupted_ = true;
currentTimerThread_.join();
isInterrupted_ = false;
return timeRemains_;
}
bool Timer::isTimerRunning()
{
return currentTimerThread_.joinable();
}