C++ windows定时器

mytime.h

#include<iostream>
#include<list>
class MyTimer {
public:
    MyTimer();
    ~MyTimer();
    typedef void(*f)();
    struct TimerInfo {
        int timerid;
        long timerout;
        long timerstart;
        bool isvalid;
        f task;
        TimerInfo(int timerid, long timerout, long timerstart, f task) {
            this->timerid = timerid;
            this->timerout = timerout;
            this->timerstart = timerstart;
            this->task = task;
            isvalid = true;
        }
    };
    int AddTimerHandler(int timeout, f task);
    void RemoveTimerHandler(const int timerid);
    void updateTimer();
    static int getTime();
    int CreatTimerId();
public:
    int Timerid;
    long Time;

private:
    std::list<TimerInfo> m_timerPool;
    long m_lastid;
};

mytime.cpp

#include"mytime.h"
#include<Windows.h>

MyTimer::MyTimer() {
    m_timerPool.clear();
}
MyTimer::~MyTimer() {
    m_timerPool.clear();
}
int MyTimer::AddTimerHandler(int timeout, f task) {
    int timeid = CreatTimerId();
    m_timerPool.push_back(TimerInfo(timeid, timeout, getTime(), task));
    return timeid;
}
void MyTimer::RemoveTimerHandler(const int timerid) {
    auto it = m_timerPool.begin();
    for (; it != m_timerPool.end(); it++) {
        if (Timerid == it->timerid)
        {
            it->isvalid = false;
            break;
        }
        else {
            it++;
        }
    }
}
void MyTimer::updateTimer() {
    auto it = m_timerPool.begin();
    long currenttime = getTime();
    for (; it != m_timerPool.end();) {
        if (it->isvalid == false)
        {
            it = m_timerPool.erase(it);
            continue;
        }
        if (currenttime >= (it->timerout + it->timerstart))
        {
            it->task();//执行定时器任务
            it = m_timerPool.erase(it);
        }
        else {
            it++;
        }
    }
}
int MyTimer::getTime() {
    tm tms;
    SYSTEMTIME sys;
    GetLocalTime(&sys);

    tms.tm_year = sys.wYear - 1900;
    tms.tm_mon = sys.wMonth;
    tms.tm_mday = sys.wDay;
    tms.tm_hour = sys.wHour;
    tms.tm_min = sys.wMinute;
    tms.tm_sec = sys.wSecond;
    tms.tm_isdst = -1;
    long clock = mktime(&tms);
    if (clock == -1)
    {
        std::cout << "Timer Error \n";
        return -1;
    }
    else
        clock = clock * 1000 + sys.wMilliseconds;
    return clock;
}
int MyTimer::CreatTimerId() {
    m_lastid = m_lastid % INT32_MAX;
    if (m_lastid == 0)
        m_lastid = 1;
    m_lastid++;
    return m_lastid;
}

main.cpp

#include <iostream>
#include"mytime.h"
#include<string>
#include <io.h>
#include <windows.h>
#include <fstream>
#include<vector>
using namespace std;
using namespace cv;
MyTimer timer;
long id1;
long id2;


//任务1
void task1() {
    //任务1
    id1 = timer.AddTimerHandler(1 * 1000, task1);//task1()重新加入定时器
}

//任务2
void task2() {
    //任务2
    id2 = timer.AddTimerHandler(1 * 1000, task2);//task2()重新加入定时器
}


int main()
{
    id1 = timer.AddTimerHandler(1 * 1000, task1);
    id2 = timer.AddTimerHandler(1 * 1000, task2);
    while (1) {
        timer.updateTimer();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天生_13

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值