博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows 非窗口定时器
阅读量:5105 次
发布时间:2019-06-13

本文共 5684 字,大约阅读时间需要 18 分钟。

#ifndef _MYTIMER_H_#define _MYTIMER_H_#include #include 
#include
#include
#include
#include "ttype.h"#include "lock.h"#pragma comment(lib, "Winmm.lib")#define TIMER_PERIOD 500class CMyTimer;enum emTimeType{ E_TIME_ONESHOT = 0, E_TIME_PERIOD = 1};struct TIMERVAL{ UINT uType; UINT uTimeVal; UINT uTimeFinal; CMyTimer *pCTimer;};typedef std::map
TimerMap; class CMyTimer{public: CMyTimer(void); ~CMyTimer(void);public: void KillMyTimer(); void SetOneTimer(UINT nTimeID,UINT nElapse); void SetTimer(UINT nTimeID,UINT nElapse); static void SetMyTimerOnce(UINT nElapse,DWORD dwUser = NULL); static void SetMyTimerPeriod(UINT nElapse,DWORD dwUser = NULL); virtual bool ontime(UINT nTimeID) = 0; static CLock m_lock; static bool m_bInitTime;private: MMRESULT m_nTimerID; static void CALLBACK OnTime(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2); static TimerMap TimerMap;};#endif
#include "MyTimer.h"#include 
#include
using namespace std;CMyTimer::CMyTimer(void){ m_nTimerID = 0;}CMyTimer::~CMyTimer(void){}TimerMap CMyTimer::TimerMap;bool CMyTimer::m_bInitTime = false;CLock CMyTimer::m_lock;void CMyTimer::SetMyTimerOnce(UINT nElapse,DWORD dwUser){ if (m_bInitTime) { return; } if (NULL != ::timeSetEvent(nElapse,1,OnTime,dwUser,TIME_ONESHOT)) { m_bInitTime = true; }}void CMyTimer::SetMyTimerPeriod(UINT nElapse,DWORD dwUser){ if (NULL != ::timeSetEvent(nElapse,1,OnTime,dwUser,TIME_PERIODIC)) { m_bInitTime = true; }}void CALLBACK CMyTimer::OnTime(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2){ CSignedLock lock(&m_lock); for (auto iter = TimerMap.begin(); iter != TimerMap.end();) { iter->second.uTimeVal += TIMER_PERIOD; if (iter->second.uTimeVal >= iter->second.uTimeFinal) { if (E_TIME_ONESHOT == iter->second.uType) { iter->second.pCTimer->ontime(iter->first); TimerMap.erase(iter++); continue; } if (E_TIME_PERIOD == iter->second.uType) { iter->second.uTimeVal = 0; if (false == iter->second.pCTimer->ontime(iter->first)) { TimerMap.erase(iter++); continue; } } } iter++; } return;}void CMyTimer:: KillMyTimer(){ ::timeKillEvent(m_nTimerID); m_bInitTime = false;}void CMyTimer::SetOneTimer(UINT nTimeID,UINT nElapse){ CSignedLock lock(&m_lock); auto iter = TimerMap.find(nTimeID); if (iter == TimerMap.end()) { TimerMap[nTimeID].pCTimer = this; TimerMap[nTimeID].uTimeFinal = nElapse; TimerMap[nTimeID].uTimeVal = 0; TimerMap[nTimeID].uType = E_TIME_ONESHOT; }}void CMyTimer::SetTimer(UINT nTimeID,UINT nElapse){ CSignedLock lock(&m_lock); auto iter = TimerMap.find(nTimeID); if (iter == TimerMap.end()) { TimerMap[nTimeID].pCTimer = this; TimerMap[nTimeID].uTimeFinal = nElapse; TimerMap[nTimeID].uTimeVal = 0; TimerMap[nTimeID].uType = E_TIME_PERIOD; }}
#ifndef _Work_H_#define _Work_H_#include "MyTimer.h"enum MyEnum{    E_TIME_ID_1 = 1,    E_TIME_ID_2 = 2,    E_TIME_ID_3 = 3,};class Work :public CMyTimer{public:    Work(void);    ~Work(void);    bool Init();    void Run();    bool unInit();    void onTime();    bool time_fun_1();    bool time_fun_2();    bool time_fun_3();    bool ontime(UINT nTimeID);};#endif//_Work_H_
#include "Work.h"#include 
using namespace std;Work::Work(void){}Work::~Work(void){}bool Work::Init(){ if (!CMyTimer::m_bInitTime) { CMyTimer::SetMyTimerPeriod(TIMER_PERIOD); } return true;}void Work::Run(){ SetOneTimer(E_TIME_ID_1,1000); SetTimer(E_TIME_ID_2,1000);}bool Work::unInit(){ return true;}bool Work::time_fun_1(){ std::cout<<"只执行一次的定时器"<<__LINE__<
///普通锁定类#ifndef _CLOCK_H_#define _CLOCK_H_#include 
class CLock{ friend class CSignedLock;private: CRITICAL_SECTION m_csLock; ///关键段 ///函数定义public: ///构造函数 inline CLock() { ::InitializeCriticalSection(&m_csLock); } ///析构函数 inline ~CLock() { ::DeleteCriticalSection(&m_csLock); } ///功能函数private: ///锁定函数 inline void Lock() { ::EnterCriticalSection(&m_csLock); } ///解锁函数 inline void UnLock() { ::LeaveCriticalSection(&m_csLock); }};class CSignedLock{ ///变量定义private: CLock * m_pLock; ///锁定对象 int m_iLockCount; ///锁定计数 ///函数定义public: ///构造函数 CSignedLock(CLock * pLock) { m_iLockCount = 0; m_pLock = pLock; Lock(); } ///析构函数 ~CSignedLock() { UnLock(); }public: ///锁定函数 inline void Lock() { if (m_pLock) { m_iLockCount++; m_pLock->Lock(); } } ///解锁函数 inline void UnLock() { if (m_pLock) { m_iLockCount--; m_pLock->UnLock(); } }};#endif //_CLOCK_H_
#ifndef _TTYPE_H_#define _TTYPE_H_typedef  unsigned int UINT;#define  saferelease(p) {\    if(nullptr != p) {\        delete p ; \        p = nullptr;\    }\}#endif
#include "work.h"#include 
#include
using namespace std;Work work ;int main(){ work.Init(); work.Run(); system("pause"); cout<

 

转载于:https://www.cnblogs.com/wanzaixiaoxin/p/4940678.html

你可能感兴趣的文章
新手算法学习之路----二叉树(在一个二叉查找树中插入一个节点)
查看>>
autopep8
查看>>
GIT在Linux上的安装和使用简介
查看>>
java 类型转型
查看>>
基于C#编程语言的Mysql常用操作
查看>>
【转】Java反射 之 反射基础
查看>>
mysql数据库备份和还原的常用命令
查看>>
s3c2440实验---定时器
查看>>
HBase配置性能调优(转)
查看>>
MyEclipse10安装SVN插件
查看>>
[转]: 视图和表的区别和联系
查看>>
Regular Experssion
查看>>
python中的字符编码
查看>>
图论例题1——NOIP2015信息传递
查看>>
uCOS-II中的任务切换-图解多种任务调度时机与问题
查看>>
CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)
查看>>
Android 官方新手指导教程
查看>>
幸运转盘v1.0 【附视频】我的Android原创处女作,请支持!
查看>>
UseIIS
查看>>
为什么int型最大的数是2147483647
查看>>