« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
谦卑,荣誉,牺牲,英勇,怜悯,诚实,精神,公正。 |
Blog信息 |
blog名称: 日志总数:183 评论数量:698 留言数量:7 访问次数:3025014 建立时间:2005年12月29日 |

| |
[试验进度]一个很炫的定时器。 读书笔记, 软件技术
newqiang 发表于 2006/5/7 19:33:15 |
/////CCounterTimer.h//////
class CCounterTimer;
class CCounterTimerListener{public: virtual void Update(CCounterTimer* Timer) = 0;};
class CCounterTimer {public: CCounterTimer(); virtual ~CCounterTimer(); /// Copying not allowed CCounterTimer(const CCounterTimer &); CCounterTimer &operator = (const CCounterTimer &);
void AttachListener(CCounterTimerListener* pListener); void Step(); void Stop(); void Start(UINT nCount); BOOL IsRuning() { return m_bRunning; };
private: UINT m_nCount; BOOL m_bRunning;
CCounterTimerListener* m_pListener;};
///////CCounterTimer.cpp////////
CCounterTimer::CCounterTimer(): m_nCount(0), m_bRunning(FALSE){
}
CCounterTimer::~CCounterTimer(){
}
void CCounterTimer::Start(UINT nCount){ m_bRunning = TRUE; m_nCount = nCount;}
void CCounterTimer::Stop(){ m_bRunning = FALSE; m_nCount = 0;}
void CCounterTimer::Step(){ if (!m_bRunning || m_nCount == 0) { return; }
m_nCount--; if (m_nCount == 0) { Stop(); m_pListener->Update(this); }}
void CCounterTimer::AttachListener(CCounterTimerListener* pListener){ m_pListener = pListener; (dynamic_cast<CDecisionMakingx*>(pListener))->m_vecTimer.push_back(this);}
///////////////////////// OVER ////////////////////////
我们可以定义一个Class CD : CCounterTimerListener {}
在CD中:
///< 周期定时器处理 CCounterTimer m_timerKick; vector<CCounterTimer*> m_vecTimer; void Update(CCounterTimer* Timer);
在CD功能初始化中:
m_timerKick.AttatchListener(this);//把m_timerKick加入到m_vecTimer中;也可以在增加别的CCounterTimer对象。
// 计时器计时 for (vector<CCounterTimer*>::iterator it = m_vecTimer.begin(); it != m_vecTimer.end(); ++it) (*it)->Step(); //对所有的CCounterTimer对象Step().
在UpDate(CCounterTimer Timer)函数中判断
if(Timer == m_timerKick){Do what you want to do!}
然后在某个函数中添加 m_timerKick.Start(20);
这样20个系统周期之后,You can -> {Do what you want to do!}
有点意思。 |
|
|