多数据源中间件标准版1.0

TemplateTimer.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <atlbase.h>
  3. static void CALLBACK TimerProc(void*, BOOLEAN);
  4. ///////////////////////////////////////////////////////////////////////////////
  5. //
  6. // class CTimer
  7. //
  8. class CTimer
  9. {
  10. public:
  11. CTimer()
  12. {
  13. m_hTimer = NULL;
  14. m_mutexCount = 0;
  15. m_bTimerStart = false;
  16. }
  17. virtual ~CTimer()
  18. {
  19. Stop();
  20. }
  21. bool Start(unsigned int interval, // interval in ms
  22. bool immediately = false,// true to call first event immediately
  23. bool once = false, // true to call timed event only once
  24. ULONG Flags = WT_EXECUTEINTIMERTHREAD)
  25. {
  26. if( m_hTimer )
  27. {
  28. return false;
  29. }
  30. SetCount(0);
  31. BOOL success = CreateTimerQueueTimer( &m_hTimer,
  32. NULL,
  33. TimerProc,
  34. this,
  35. immediately ? 0 : interval,
  36. once ? 0 : interval,
  37. Flags);
  38. if (success != 0)
  39. {
  40. m_bTimerStart = true;
  41. }
  42. return (success != 0);
  43. }
  44. void Stop()
  45. {
  46. m_bTimerStart = false;
  47. DeleteTimerQueueTimer( NULL, m_hTimer, NULL );
  48. m_hTimer = NULL ;
  49. }
  50. void StopSafely()
  51. {
  52. m_bTimerStart = false;
  53. DeleteTimerQueueTimer( NULL, m_hTimer, INVALID_HANDLE_VALUE );
  54. m_hTimer = NULL ;
  55. }
  56. virtual void OnTimedEvent()
  57. {
  58. // Override in derived class
  59. }
  60. void SetCount(int value)
  61. {
  62. InterlockedExchange( &m_mutexCount, value );
  63. }
  64. int GetCount()
  65. {
  66. return InterlockedExchangeAdd( &m_mutexCount, 0 );
  67. }
  68. bool IsTimerStart(){return m_bTimerStart;}
  69. private:
  70. HANDLE m_hTimer;
  71. long m_mutexCount;
  72. bool m_bTimerStart;
  73. };
  74. ///////////////////////////////////////////////////////////////////////////////
  75. //
  76. // TimerProc
  77. //
  78. void CALLBACK TimerProc(void* param, BOOLEAN timerCalled)
  79. {
  80. CTimer* timer = static_cast<CTimer*>(param);
  81. timer->SetCount( timer->GetCount()+1 );
  82. timer->OnTimedEvent();
  83. };
  84. ///////////////////////////////////////////////////////////////////////////////
  85. //
  86. // template class TTimer
  87. //
  88. template <class T> class TTimer : public CTimer
  89. {
  90. public:
  91. typedef private void (T::*TimedFunction)(void);
  92. TTimer()
  93. {
  94. m_pTimedFunction = NULL;
  95. m_pClass = NULL;
  96. }
  97. void SetTimedEvent(T *pClass, TimedFunction pFunc)
  98. {
  99. m_pClass = pClass;
  100. m_pTimedFunction = pFunc;
  101. }
  102. protected:
  103. void OnTimedEvent()
  104. {
  105. if (m_pTimedFunction && m_pClass)
  106. {
  107. (m_pClass->*m_pTimedFunction)();
  108. }
  109. }
  110. private:
  111. T *m_pClass;
  112. TimedFunction m_pTimedFunction;
  113. };