Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1753906
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: WINDOWS

2011-12-21 11:09:50

该定时器类继承一个Thread类,在线程函数里,每隔一定的时间,执行一次TimerHandler.

Thread.h

  1. #ifndef _THREAD_H_
  2. #define _THREAD_H_

  3. #include <Windows.h>

  4. class Thread
  5. {
  6. public:
  7.     Thread();
  8.     virtual ~Thread();

  9.     virtual void    Run() = 0;
  10.     void            Start();
  11.     void            Stop();
  12.     bool            IsStop();

  13. protected:
  14.     static DWORD WINAPI ThreadProc(LPVOID p);

  15. private:
  16.     bool    m_stopFlag;
  17.     HANDLE m_hThread;
  18. };

  19. #endif

Thread.cpp

  1. #include "Thread.h"

  2. Thread::Thread()
  3.     :m_stopFlag(false)
  4.     ,m_hThread(INVALID_HANDLE_VALUE)
  5. {
  6. }

  7. Thread::~Thread()
  8. {
  9.     Stop();
  10. }

  11. void Thread::Start()
  12. {
  13.     unsigned long *p =NULL;
  14.     m_hThread = ::CreateThread(NULL, 0, ThreadProc, this, 0, p);
  15. }

  16. DWORD WINAPI Thread::ThreadProc(LPVOID p)
  17. {
  18.     Thread* thread = (Thread*)p;
  19.     thread->Run();

  20.     CloseHandle( thread->m_hThread );
  21.     thread->m_hThread= INVALID_HANDLE_VALUE;

  22.     return 0;
  23. }

  24. void Thread::Stop()
  25. {
  26.     m_stopFlag = true;

  27.     if(m_hThread != INVALID_HANDLE_VALUE)
  28.     {
  29.         if(WaitForSingleObject(m_hThread,INFINITE) != WAIT_ABANDONED)
  30.         {
  31.             CloseHandle(m_hThread);
  32.         }
  33.         m_hThread = INVALID_HANDLE_VALUE;
  34.     }
  35. }


  36. bool Thread::IsStop()
  37. {
  38.     return m_stopFlag;
  39. }

Timer.h

  1. #ifndef _TIMER_H_
  2. #define _TIMER_H_

  3. #include <Windows.h>
  4. #include "Thread.h"

  5. class Timer : public Thread
  6. {
  7.     typedef void(CALLBACK *Timerfunc)(void* p);
  8.     typedef Timerfunc TimerHandler;
  9. public:
  10.     Timer()
  11.         :m_handler(0)
  12.         ,m_interval(-1)
  13.     {
  14.     }

  15.     void registerHandler(TimerHandler handler, void* p)
  16.     {
  17.         m_handler = handler;
  18.         m_parameter = p;
  19.     }

  20.     void setInterval(int millisecond)
  21.     {
  22.         m_interval = millisecond;
  23.     }

  24.     void Run()
  25.     {
  26.         unsigned long tickNow = ::GetTickCount();
  27.         unsigned long tickLastTime = tickNow;

  28.         while(!IsStop())
  29.         {
  30.             tickNow = ::GetTickCount();
  31.             if(tickNow - tickLastTime > m_interval)
  32.             {
  33.                 if(m_handler)
  34.                 {
  35.                     (*m_handler)(m_parameter);
  36.                 }
  37.                 tickLastTime = ::GetTickCount();
  38.             }

  39.             ::Sleep(1);
  40.         }
  41.     }

  42.     void Cancel()
  43.     {
  44.         Stop();
  45.     }

  46. private:
  47.     TimerHandler m_handler;
  48.     int             m_interval;
  49.     void*         m_parameter;
  50. };

  51. #endif

test.cpp

  1. #include <iostream>
  2. #include <Windows.h>
  3. #include "Timer.h"

  4. using namespace std;


  5. void CALLBACK TimerProc(void* p)
  6. {
  7.     int i = *((int*)p);
  8.     cout << i << endl;
  9. }

  10. void main()
  11. {
  12.     Timer timer;

  13.     int i = 10;

  14.     int *p = &i;

  15.     timer.registerHandler(TimerProc, p);
  16.     timer.setInterval(1000);
  17.     timer.Start();

  18.     ::Sleep(1000);

  19.     for(; i > 0; i--)
  20.     {
  21.         cout << "hello" << endl;
  22.         ::Sleep(1000);
  23.     }

  24.     timer.Cancel();
  25.     system("pause");
  26. }

运行结果

hello
10
hello
9
hello
8
hello
7
hello
6
hello
5
hello
4
hello
3
hello
2
hello
1

阅读(29733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~