运行在C++应用程序后面的C++计时器
C++ Timer that runs at back of an C++ application
我有一个在前台运行的C++应用程序。我需要一个与应用程序同时运行的计时器。当计时器达到零时,我需要计时器弹出一个窗口。
我不能使用sleep(),因为who
应用程序处于睡眠状态。请就如何做到这一点提供建议。
由于您使用的是C++11,我建议使用thread
库。
您可能想要的是std::this_thread::sleep_for
或std::this_thread::sleep_until
,它们可以在定时器线程的上下文中调用。
像这样的。。。
std::thread timer([]() {
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "hello, world!" << std::endl;
});
std::cout << "thread begun..." << std::endl;
timer.join();
我建议下载Boost库,然后使用这个关于创建Boost线程的非常简单的教程。
如果您不想花时间下载/安装/配置Boost,请使用Windows线程。(我假设您正在尝试使用Windows上的sleep()
)。不过,Windows线程比Boost线程更难理解。
在实际的程序中,你会想要包含这样的东西(以Boost为例):
void timer() {
sleep(x);
//Whatever code here to make your popup window.
return NULL;
}
int main() {
boost::thread prgmTimer(&timer);
//Regular code here.
//prgmTimer.join(); //Remove the comment on that command if you want something to
//to happen after your timer runs down and only if your
//timer runs down. (Ex. the program exits).
return 0;
}
相关文章:
- Linux的Cpp上的计时器
- 提升 ASIO 无法识别计时器对象
- 提升 asio 并发计时器取消问题与链
- 使用单体计时器的pthread_cond_timedwait有时会比预期晚超时
- 窗口中的微秒计时器
- 计时器是否从另一个线程启动?
- 如何在 c++ 中创建计时器
- C++回调计时器实现
- 在计时器或主线程外部的命令上销毁/替换线程
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 保留计时器集合(对象与指针)的最佳方法
- 在网络套接字计时器滴答后增加asio短读错误
- 是否可以仅使用标准 c++/c++11 实现不带"sleep"的计时器?
- 在没有NtSetTimerResolution的Windows上提高计时器分辨率(高分辨率)
- 计时器坏了或者其他什么的
- 功能计时器阻止主功能继续
- IO服务重新启动后,Boost最后期限计时器持续触发
- boost asio计时器是否会在"取消"时阻塞
- Poco 计时器,具有来自同一类的回调
- 使计时器在C++进程中的特定时间关闭,以便同步两个进程