EventThread.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "StdAfx.h"
  2. #include "EventThread.h"
  3. #define EXIT_EVENT -1
  4. CEventThread::CEventThread(void)
  5. {
  6. AddEvent(EXIT_EVENT);
  7. m_hEvt = CreateEvent(NULL, FALSE, FALSE, NULL);
  8. m_waitTimeout = 1000;
  9. m_threadRunning = false;
  10. m_exitThread = false;
  11. }
  12. CEventThread::~CEventThread(void)
  13. {
  14. Stop();
  15. for(EventMapType::iterator it = m_eventMap.begin(); it != m_eventMap.end(); it++)
  16. {
  17. CloseHandle(it->first);
  18. }
  19. }
  20. UINT CEventThread::EventThreadFnc(void* thisptr)
  21. {
  22. CEventThread *threadClass = (CEventThread*)thisptr;
  23. threadClass->RunThread();
  24. return 0;
  25. }
  26. void CEventThread::AddEvent(int eventId)
  27. {
  28. HANDLE handle = CreateEvent(NULL, FALSE, FALSE, _T(""));
  29. m_eventMap[handle] = eventId;
  30. }
  31. bool CEventThread::FireEvent(int eventId)
  32. {
  33. HANDLE eventHandle = NULL;
  34. for(EventMapType::iterator it = m_eventMap.begin(); it != m_eventMap.end(); it++)
  35. {
  36. if(it->second == eventId)
  37. {
  38. eventHandle = it->first;
  39. break;
  40. }
  41. }
  42. if(eventHandle != NULL)
  43. {
  44. SetEvent(eventHandle);
  45. return true;
  46. }
  47. return false;
  48. }
  49. bool CEventThread::UndoFireEvent(int eventId)
  50. {
  51. HANDLE eventHandle = NULL;
  52. for(EventMapType::iterator it = m_eventMap.begin(); it != m_eventMap.end(); it++)
  53. {
  54. if(it->second == eventId)
  55. {
  56. eventHandle = it->first;
  57. break;
  58. }
  59. }
  60. if(eventHandle != NULL)
  61. {
  62. ResetEvent(eventHandle);
  63. return true;
  64. }
  65. return false;
  66. }
  67. void CEventThread::Start(void *param)
  68. {
  69. if(m_threadRunning == false)
  70. {
  71. ResetEvent(m_hEvt);
  72. m_exitThread = false;
  73. m_param = param;
  74. m_thread = (HANDLE)_beginthreadex(NULL, 0, EventThreadFnc, this, 0, &m_threadID);
  75. // now wait until the thread is up and really running
  76. WaitForSingleObject(m_hEvt, 1000);
  77. }
  78. else
  79. {
  80. UndoFireEvent(EXIT_EVENT);
  81. }
  82. }
  83. void CEventThread::Stop(int waitTime)
  84. {
  85. if(m_threadRunning)
  86. {
  87. m_exitThread = true;
  88. FireEvent(EXIT_EVENT);
  89. if(waitTime > 0)
  90. {
  91. if (WAIT_OBJECT_0 != WaitForSingleObject(m_hEvt, waitTime))
  92. {
  93. TerminateThread(m_thread, 0);
  94. m_threadRunning = false;
  95. }
  96. }
  97. }
  98. };
  99. void CEventThread::RunThread()
  100. {
  101. m_threadRunning = true;
  102. HANDLE *pHandleArray = new HANDLE[m_eventMap.size()];
  103. int indexPos = 0;
  104. for(EventMapType::iterator it = m_eventMap.begin(); it != m_eventMap.end(); it++)
  105. {
  106. pHandleArray[indexPos] = it->first;
  107. indexPos++;
  108. }
  109. SetEvent(m_hEvt);
  110. ResetEvent(m_hEvt);
  111. while(true)
  112. {
  113. DWORD event = WaitForMultipleObjects((DWORD)m_eventMap.size(), pHandleArray, FALSE, m_waitTimeout);
  114. if(event == WAIT_FAILED)
  115. {
  116. LPVOID lpMsgBuf = NULL;
  117. DWORD dwErr = GetLastError();
  118. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  119. FORMAT_MESSAGE_FROM_SYSTEM |
  120. FORMAT_MESSAGE_IGNORE_INSERTS,
  121. NULL,
  122. dwErr,
  123. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  124. (LPTSTR) &lpMsgBuf,
  125. 0,
  126. NULL);
  127. ASSERT(!lpMsgBuf);
  128. LocalFree(lpMsgBuf);
  129. }
  130. else if(event == WAIT_TIMEOUT)
  131. {
  132. OnTimeOut(m_param);
  133. }
  134. else
  135. {
  136. HANDLE firedHandle = pHandleArray[event - WAIT_OBJECT_0];
  137. int eventId = m_eventMap[firedHandle];
  138. if(eventId == EXIT_EVENT)
  139. {
  140. SetEvent(m_hEvt);
  141. break;
  142. }
  143. else
  144. {
  145. OnEvent(eventId, m_param);
  146. }
  147. }
  148. }
  149. m_threadRunning = false;
  150. }