诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

217 lines
4.7 KiB

  1. #include "StdAfx.h"
  2. #include "WHThread.h"
  3. //////////////////////////////////////////////////////////////////////////////////
  4. //结构定义
  5. //启动参数
  6. struct tagThreadParameter
  7. {
  8. bool bSuccess; //是否错误
  9. HANDLE hEventFinish; //事件句柄
  10. CWHThread * pServiceThread; //线程指针
  11. };
  12. //////////////////////////////////////////////////////////////////////////////////
  13. //构造函数
  14. CWHThread::CWHThread()
  15. {
  16. //设置变量
  17. m_bRun = false;
  18. m_uThreadID = 0;
  19. m_hThreadHandle = NULL;
  20. return;
  21. }
  22. //析构函数
  23. CWHThread::~CWHThread()
  24. {
  25. //停止线程
  26. ConcludeThread(INFINITE);
  27. return;
  28. }
  29. //状态判断
  30. bool CWHThread::IsRuning()
  31. {
  32. //运行检测
  33. if (m_hThreadHandle == NULL) return false;
  34. if (WaitForSingleObject(m_hThreadHandle, 0) != WAIT_TIMEOUT) return false;
  35. return true;
  36. }
  37. //启动线程
  38. bool CWHThread::StartThread()
  39. {
  40. //效验状态
  41. ASSERT(IsRuning() == false);
  42. if (IsRuning() == true) return false;
  43. //清理变量
  44. if (m_hThreadHandle != NULL)
  45. {
  46. //关闭句柄
  47. CloseHandle(m_hThreadHandle);
  48. //设置变量
  49. m_uThreadID = 0;
  50. m_hThreadHandle = NULL;
  51. }
  52. //变量定义
  53. tagThreadParameter ThreadParameter;
  54. ZeroMemory(&ThreadParameter, sizeof(ThreadParameter));
  55. //设置变量
  56. ThreadParameter.bSuccess = false;
  57. ThreadParameter.pServiceThread = this;
  58. ThreadParameter.hEventFinish = CreateEvent(NULL, FALSE, FALSE, NULL);
  59. //效验状态
  60. ASSERT(ThreadParameter.hEventFinish != NULL);
  61. if (ThreadParameter.hEventFinish == NULL) return false;
  62. //启动线程
  63. m_bRun = true;
  64. m_hThreadHandle = (HANDLE)::_beginthreadex(NULL, 0, ThreadFunction, &ThreadParameter, 0, &m_uThreadID);
  65. //错误判断
  66. if (m_hThreadHandle == INVALID_HANDLE_VALUE)
  67. {
  68. CloseHandle(ThreadParameter.hEventFinish);
  69. return false;
  70. }
  71. //等待事件
  72. WaitForSingleObject(ThreadParameter.hEventFinish, INFINITE);
  73. CloseHandle(ThreadParameter.hEventFinish);
  74. //判断错误
  75. if (ThreadParameter.bSuccess == false)
  76. {
  77. ConcludeThread(INFINITE);
  78. return false;
  79. }
  80. return true;
  81. }
  82. //停止线程
  83. bool CWHThread::ConcludeThread(DWORD dwMillSeconds)
  84. {
  85. //停止线程
  86. if (IsRuning() == true)
  87. {
  88. //设置变量
  89. m_bRun = false;
  90. //停止等待
  91. if (WaitForSingleObject(m_hThreadHandle, dwMillSeconds) == WAIT_TIMEOUT)
  92. {
  93. return false;
  94. }
  95. }
  96. //设置变量
  97. if (m_hThreadHandle != NULL)
  98. {
  99. //关闭句柄
  100. CloseHandle(m_hThreadHandle);
  101. //设置变量
  102. m_uThreadID = 0;
  103. m_hThreadHandle = NULL;
  104. }
  105. return true;
  106. }
  107. //投递消息
  108. LRESULT CWHThread::PostThreadMessage(UINT uMessage, WPARAM wParam, LPARAM lParam)
  109. {
  110. //状态效验
  111. ASSERT((m_uThreadID != 0) && (m_hThreadHandle != NULL));
  112. if ((m_uThreadID == 0) || (m_hThreadHandle == NULL)) return false;
  113. //投递消息
  114. if (::PostThreadMessage(m_uThreadID, uMessage, wParam, lParam) == FALSE)
  115. {
  116. DWORD dwLastError = GetLastError();
  117. return dwLastError;
  118. }
  119. return 0L;
  120. }
  121. //线程函数
  122. unsigned __stdcall CWHThread::ThreadFunction(LPVOID pThreadData)
  123. {
  124. //随机种子
  125. srand((DWORD)time(NULL));
  126. //变量定义
  127. tagThreadParameter * pThreadParameter = (tagThreadParameter *)pThreadData;
  128. CWHThread * pServiceThread = pThreadParameter->pServiceThread;
  129. //启动通知
  130. try
  131. {
  132. pThreadParameter->bSuccess = pServiceThread->OnEventThreadStrat();
  133. }
  134. catch (...)
  135. {
  136. //设置变量
  137. ASSERT(FALSE);
  138. pThreadParameter->bSuccess = false;
  139. }
  140. //设置事件
  141. bool bSuccess = pThreadParameter->bSuccess;
  142. ASSERT(pThreadParameter->hEventFinish != NULL);
  143. if (pThreadParameter->hEventFinish != NULL) SetEvent(pThreadParameter->hEventFinish);
  144. //线程处理
  145. if (bSuccess == true)
  146. {
  147. //线程运行
  148. while (pServiceThread->m_bRun)
  149. {
  150. #ifndef _DEBUG
  151. //运行版本
  152. try
  153. {
  154. if (pServiceThread->OnEventThreadRun() == false)
  155. {
  156. break;
  157. }
  158. }
  159. catch (...) {}
  160. #else
  161. //调试版本
  162. if (pServiceThread->OnEventThreadRun() == false)
  163. {
  164. break;
  165. }
  166. #endif
  167. }
  168. //停止通知
  169. try
  170. {
  171. pServiceThread->OnEventThreadConclude();
  172. }
  173. catch (...) { ASSERT(FALSE); }
  174. }
  175. //中止线程
  176. _endthreadex(0L);
  177. return 0L;
  178. }
  179. //////////////////////////////////////////////////////////////////////////////////