C++ 每 5 分钟调用一次函数
c++ call function every 5 minutes
我想每 5 分钟
调用一次函数我试过了
AutoFunction(){
cout << "Auto Notice" << endl;
Sleep(60000*5);
}
while(1){
if(current->tm_hour == StartHour && current->tm_min == StartMinut && current->tm_sec == StartSec){
CallStart();
}
AutoFunction();
Sleep(1000);
}
我想每 1 秒刷新一次while
,同时call AutoFunction()
次;每 5 分钟刷新一次,但不要在 AutoFunction 中等待Sleep
因为我必须每 1 秒刷新一次 while(1) 以检查启动另一个函数的时间
我想这样做
while(1){
if(current->tm_hour == StartHour && current->tm_min == StartMinut && current->tm_sec == StartSec){
CallStart();
}
Sleep(1000);
}
while(1){
AutoFunction();
Sleep(60000*5);
}
但我不这么认为两者都会一起工作
谢谢
对于我们这些不熟悉线程和 Boost 库的人来说,这可以通过一个 while 循环来完成:
void AutoFunction(){
cout << "Auto Notice" << endl;
}
//desired number of seconds between calls to AutoFunction
int time_between_AutoFunction_calls = 5*60;
int time_of_last_AutoFunction_call = curTime() - time_between_AutoFunction_calls;
while(1){
if (should_call_CallStart){
CallStart();
}
//has enough time elapsed that we should call AutoFunction?
if (curTime() - time_of_last_AutoFunction_call >= time_between_AutoFunction_calls){
time_of_last_AutoFunction_call = curTime();
AutoFunction();
}
Sleep(1000);
}
在此代码中,curTime
是我编造的一个函数,它将 Unix 时间戳作为 int 返回。
相关文章:
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何为非常量和常量重载实现一次成员函数?
- 某些 boost::asio 异步函数是否将处理程序连接到操作,以便处理程序被触发一次?
- 如何一次运行3个函数?
- 循环中本地对象的析构函数是否保证在下一次迭代之前被调用?
- 调用函数一次用于动态链接库,一次从可执行文件调用函数
- 仅在函数中设置静态变量一次
- 有没有办法一次运行 3 个创建窗口函数?
- 在可变函数调用中执行一次语句
- 为什么转换运算符调用复制构造函数两次,而等效函数只调用它一次
- 为什么一次包装 typedef 函数签名与原始签名不匹配
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 我需要线程函数在不停止实际程序的情况下,每2秒打印一次随机数
- Android JNI RegisterNatives:对所有内容调用一次,或者每个函数调用一次
- 如何在 constexpr 构造函数中初始化矩阵一次?
- cppUnit:针对多个测试方法执行一次的设置函数
- 为什么调用 cout.operator<<(const char*) 打印地址而不是字符串? 如何创建一个函数本地静态"HashSet<char>"并初始化它一次?
- 跨类的多个实例调用函数一次
- C++一次将函数标记为静态就足够了?