-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.cpp
75 lines (57 loc) · 1.23 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include "conio.h"
#include "TinyTimer.hpp"
using namespace std;
void Print()
{
std::cout << "定时器全局函数Print执行" << std::endl;
}
class Task
{
public:
Task() {
};
~Task() {
};
void TaskRun()
{
std::cout << "Task::TaskRun()函数执行" << std::endl;
};
};
int main()
{
TinyTimer::ptr tinyTimer = std::make_shared<TinyTimer>();
// 延迟1秒执行1次静态/全局函数
tinyTimer->AsyncOnceExecute(1000, Print);
tinyTimer->KillTimer();
// 延迟1秒执行lambda函数表达式
tinyTimer->AsyncOnceExecute(1000, []() {
std::cout << "Lambda函数执行" << std::endl;
});
tinyTimer->KillTimer();
// 延迟1秒执行1次类成员函数
Task taskObj;
std::function<void(void)> taskRunFunc = std::bind(&Task::TaskRun, taskObj);
tinyTimer->AsyncOnceExecute(1000, taskRunFunc);
tinyTimer->KillTimer();
// 循环执行静态/全局函数,按Q键杀死定时器
int a = 0;
while (true)
{
// 在此处填入需要循环的代码
if (a == 0)
{
tinyTimer->AsyncLoopExecute(1, Print);
}
if (_kbhit()) // 如果有按键被按下
{
if (_getch() == 'q') //如果按下了q键则跳出循环
{
tinyTimer->KillTimer();
break;
}
}
}
getchar();
return 0;
}