当前位置: 首页 > news >正文

实现定时器的两种方法:使用windows api定时器 和使用c++11/14 定时器

前言:

当我有一个开发需求,符合下面的条件

1.需要某个任务在程序中每隔一段时间就要执行一次,可能把这个任务封装成了一个函数。

2.这种需要定时执行的任务,有2个,3个....越来越多

这个时候我们就可以考虑使用定时器,把这种任务封装成函数,放进定时器中。每隔一段时间会自动帮我们执行该任务。

1.windows api定时器

主要是使用了两个windows api 函数,来实现定时器的效果

SetTimer函数和KillTimer函数

/** Windows Functions*/WINUSERAPI
UINT_PTR
WINAPI
SetTimer(_In_opt_ HWND hWnd, //窗口句柄_In_ UINT_PTR nIDEvent,//注册的对应任务的ID,_In_ UINT uElapse, //设置的每次执行该回调函数的时间间隔,单位是毫秒_In_opt_ TIMERPROC lpTimerFunc); //注册的对应任务的回调函数,

删除某个定时器里面的任务。

WINUSERAPI
BOOL
WINAPI
KillTimer(_In_opt_ HWND hWnd, // 窗口句柄_In_ UINT_PTR uIDEvent); //对应的定时任务的id

来一个实际的Demo:

#include <iostream>
#include <Windows.h>using namespace std;void CALLBACK Task1(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
{cout << "task1" << endl;
}void CALLBACK Task2(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
{cout << "task2" << endl;
}void main()
{MSG msg;SetTimer(NULL, 111, 1000, (TIMERPROC)Task1);SetTimer(NULL, 112, 1000, (TIMERPROC)Task2);//消息死循环,一直监听键盘的输入,然后把消息发送到主程序窗口//我们按下ctrl + c的时候,程序会自动停止while (GetMessage(&msg, NULL, NULL, NULL)){if (msg.message == WM_TIMER){TranslateMessage(&msg); //把键盘字符,转换成协议消息DispatchMessage(&msg);//把消息命令发送到主窗口}}KillTimer(NULL, 111);KillTimer(NULL, 112);
}

输出打印结果:

过程说明:

windows主程序中已经帮我们写好了一个定时器的组件。

我们只需要把我们要执行的任务,封装成回调函数。

然后通过SetTimer把这个函数注册进去就行。

通过KillTimer把某个任务删掉就行。

2.c++11/14 实现定时器----简易定时器

有两种定时器:

1.每天的固定时间执行一次任务。

2.间隔一段时候执行一任务。

task_timer.h

#pragma once#include <iostream>
#include <thread>
#include <functional>
#include <ctime>
class TaskTimer {
public:TaskTimer() {};~TaskTimer() {};
private:void ThreadInterval(int interval, std::function<void()> task){while (!stop_sign){task();std::chrono::milliseconds dura(interval);	//间隔几秒std::this_thread::sleep_for(dura);}}void ThreadFixedTime(struct tm time_data, std::function<void()> task){time_t t = time(nullptr);struct tm nowTime;while (!stop_sign){t = time(nullptr);localtime_s(&nowTime, &t);//std::cout << nowTime.tm_hour << " " << nowTime.tm_min << " " << nowTime.tm_sec << " " << std::endl;if (time_data.tm_hour == nowTime.tm_hour && time_data.tm_min == nowTime.tm_min && time_data.tm_sec == nowTime.tm_sec){task();}std::chrono::milliseconds dura(900);	std::this_thread::sleep_for(dura);}}public://添加一个任务间隔一段时间执行一次void AddTaskInterval(int interval, std::function<void()> task){std::thread( &TaskTimer::ThreadInterval, this, interval, task).detach();}//添加一个任务,在每天的固定时间执行void AddTaskFixedTime(struct tm time_data, std::function<void()> task){std::thread(&TaskTimer::ThreadFixedTime, this, time_data, task).detach();}//停止定时器void StopTaskInterval(){stop_sign = true;}private:std::atomic<bool> stop_sign = false;
};

main.cpp

#include <iostream>
#include "task_timer.h"
void func1()
{std::cout << "func1\n" << std::endl;
}void func2()
{std::cout << "func2\n" << std::endl;
}int main(int argc, char* argv[])
{TaskTimer timer;//timer.AddTaskInterval(1000, func1);//timer.AddTaskInterval(1000, func2);struct tm time_data;time_data.tm_hour = 17;time_data.tm_min = 14;time_data.tm_sec = 58;timer.AddTaskFixedTime(time_data, func1);timer.AddTaskFixedTime(time_data, func2);getchar();return 0;
}

http://www.lryc.cn/news/309345.html

相关文章:

  • H5:图像标签和路径
  • AI学习(5):PyTorch-核心模块(Autograd):自动求导
  • Grid-Based Continuous Normal Representation for Anomaly Detection 论文阅读
  • FaceBook获取广告数据
  • Redis之十:Spring Data Redis --- CrudRepository方式
  • Spring重点记录
  • 代码覆盖率工具Gcovr和Fastcov的性能对比
  • css - flex布局实现div横向滚动
  • 关于在Ubuntu20.04环境下安装GRPC
  • 力扣601 体育馆的人流量
  • ubuntu20.04设置docker容器开机自启动
  • Kubernetes/k8s的核心概念
  • vue 前端预览 Excel 表
  • 【JS】生成N位随机数
  • 2024年FPGA可以进吗
  • 小程序图形:echarts-weixin 入门使用
  • 百度百科人物创建要求是什么?
  • 练习2-线性回归迭代(李沐函数简要解析)
  • 人像背景分割SDK,智能图像处理
  • 100M服务器能同时容纳多少人访问
  • Mysql 的高可用详解
  • Acwing枚举、模拟与排序(一)
  • MySQL的主从同步原理
  • naive-ui-admin 表格去掉工具栏toolbar
  • C++之结构体
  • 分布式ID选型对比(1)
  • T-SQL 高阶语法之存储过程
  • 解决鸿蒙模拟器卡顿的问题
  • 【LeetCode每日一题】【BFS模版与例题】863.二叉树中所有距离为 K 的结点
  • 设计模式-结构模式-装饰模式