自己写的一个多进程的定时器类方便使用,放在bolg里
来源:互联网 发布:淘宝erp系统 编辑:程序博客网 时间:2024/05/16 12:01
//cMyTimer.h
#pragma once
#include "string"#include "list"
using namespace std;
struct stTimer
{
unsigned timeElapce;//定时器间隔运行时间
unsigned timeCreate;//定时器创建时间
unsigned timeLastRun;//定时器上次执行时间
unsigned id; //定时器ID号
int iParam; //预留参数
string strParam; //预留参数
bool bDel; //是否已被删除
stTimer()
{
timeCreate = 0;
timeLastRun = 0;
id = -1;
iParam = 0;
bDel = false;
}
};
typedef list<stTimer> stTimerList;
typedef list<stTimer>::iterator itTimerList;
class cMyTimer
{
public:
cMyTimer();
virtual ~cMyTimer();
//添加定时器
void AddTimer(unsigned timerId, unsigned timeMs,int param = 0,
char* p=NULL);
//删除定时器
void DeleteTimer(int id);
//定时器处理
virtual int OnTimer(int id,int iParam,string str) = 0;
//检测定时器运行
bool TimerRun();
//清除所有定时器
void ClearTimer();
//定时检测删除定时器
void CheckDelTimer();
private:
stTimerList m_timerList;//定时器列表
};
//cMyTimer.cpp
#include "stdafx.h"
#include "cMyTimer.h"
#include "windows.h"
#include "process.h"
cMyTimer::cMyTimer()
{
}
cMyTimer::~cMyTimer()
{
ClearTimer();
}
void cMyTimer::ClearTimer()
{
for (itTimerList it = m_timerList.begin();it != m_timerList.end();++it)
{
it->bDel = true;
}
}
void CheckTimerRun(void* p)
{
cMyTimer* pTimer = (cMyTimer*)p;
if (pTimer == NULL)
{
return;
}
while(1)
{
pTimer->CheckDelTimer();
//检测是否有定时器要运行
if(!pTimer->TimerRun())
{
_endthread();
}
Sleep(20);
}
}
//添加定时器
void cMyTimer::AddTimer(unsigned timerId,unsigned timeMs,int param,char* p)
{
if (timeMs == 0)
{
return;
}
stTimer stTimerTemp;
stTimerTemp.bDel = false;
stTimerTemp.id = timerId;
stTimerTemp.iParam = param;
if (p != NULL)
{
stTimerTemp.strParam = p;
}
unsigned timeNow = GetTickCount();
stTimerTemp.timeCreate = timeNow;
stTimerTemp.timeLastRun = timeNow;
stTimerTemp.timeElapce = timeMs;
m_timerList.push_back(stTimerTemp);
if (m_timerList.size() == 1)
{
//说明此时是第一个定时器
_beginthread(CheckTimerRun,0,this);
}
}
//删除定时器
void cMyTimer::DeleteTimer(int id)
{
for (itTimerList it = m_timerList.begin();it != m_timerList.end();++it)
{
if (it->id == id)
{
it->bDel = true;
return;
}
}
}
//定时器处理
//int cMyTimer::OnTimer(int id,int iParam,string str)
//{
// return 1;
//}
//定时检测删除定时器
void cMyTimer::CheckDelTimer()
{
for (itTimerList it = m_timerList.begin();it != m_timerList.end();)
{
if (it->bDel)
{
m_timerList.erase(it);
it = m_timerList.begin();
continue;
}
++it;
}
}
bool cMyTimer::TimerRun()
{
if (m_timerList.size() == 0)
{
return false;
}
unsigned timeNow = GetTickCount();
for (itTimerList it = m_timerList.begin();it != m_timerList.end();++it)
{
if (timeNow>=it->timeLastRun + it->timeElapce)
{
it->timeLastRun = timeNow;
if(OnTimer(it->id,it->iParam,it->strParam) == 0)
{
//删除定时器
it->bDel = true;
}
continue;
}
}
return true;
}
- 自己写的一个多进程的定时器类方便使用,放在bolg里
- 把百度地图放在自己所写的网页里,可放大缩小
- 把百度地图放在自己所写的网页里,可放大缩小
- C++拷贝构造函数详解(转载---原作者写的很好,不过还是放在自己这里查阅起来方便。)
- 自己写的一个定时器 timer
- 自己写一个简单的定时器settimer
- 虽然这还是一个测试阶段的BOLG,不过还是在这写了。。。
- 构建一个简单的jquery定时器,方便随时拿来使用。
- 如何用VS2010新建一个类?用来写C++文件的标准格式建立?就是能做到声把声明放在.H里,实现放在.CPP里的
- 终于有了自己的BOLG!
- 如何才能建立自己的BOLG
- 自己写的C++定时器
- 这阶段写了一个自定义的dictionary在自己的winfrom里应用
- 昨天在被窝里玩手机游戏到深夜,自己写的“扫雷”,一个字:好玩。
- 自己写的获取网络图片,不放内存里,包括文件的删除,创建
- 写了一个定时器类,以方便应用
- 自己写的动画函数,方便啊
- 在C++里写一个不能被继承的类
- 4.24
- Linux命令行与shell脚本(2)--常用命令
- android developer tiny share-20170424
- Struts2中request、response、session、application对象的访问方法
- HTML块级元素
- 自己写的一个多进程的定时器类方便使用,放在bolg里
- Android 发送无序广播
- 我的留言贴
- 初学webpack
- Android之用UncaughtExceptionHandler实现保存崩溃日志到sdcard目录下的文件夹
- HTML骨架结构
- 4.24
- [IMWeb训练营]-团队作业
- Linux学习第四单元-管理系统中的简单分区和文件系统