WorkThread.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "stdafx.h"
  2. #include "WorkThread.h"
  3. #include <windows.h>
  4. #include "msgdef.h"
  5. #include "ThreadPool.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. CWorkThread::CWorkThread()
  12. {
  13. m_lpEvent = NULL;
  14. m_pThreadPool = NULL;
  15. }
  16. CWorkThread::~CWorkThread()
  17. {
  18. m_lpEvent = NULL;
  19. m_pThreadPool = NULL;
  20. }
  21. /************************************************************************/
  22. /*
  23. 函数:AddEvent
  24. 描述:添加事项
  25. 参数:
  26. IN CONST LPSEVENT lpEvent, 工作事项
  27. IN CONST CThreadPool* p 线程池
  28. 返回:
  29. */
  30. /************************************************************************/
  31. int CWorkThread::AddEventItem(IN CONST LPSEVENT lpEvent, IN CONST CThreadPool* p)
  32. {
  33. if(lpEvent == NULL || p == NULL)
  34. return 0;
  35. m_lpEvent = lpEvent;
  36. m_lpEvent->nEventID = m_nThreadID;
  37. m_pThreadPool = (CThreadPool*)p;
  38. return 1;
  39. }
  40. /************************************************************************/
  41. /*
  42. 函数:Run
  43. 描述:运行
  44. 参数:
  45. 返回:1成功,0失败
  46. */
  47. /************************************************************************/
  48. THREADSTATUS CWorkThread::Run()
  49. {
  50. /*
  51. WaitForSingleObject(m_hWakeUpSignal, INFINITE);
  52. // 等待唤醒事件
  53. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  54. {
  55. m_ThreadStatus = THREADSTATUS_EXIT;
  56. return m_ThreadStatus;
  57. }
  58. */
  59. #ifdef _DEBUG
  60. CString strMsg = _T("");
  61. if(m_ThreadStatus == THREADSTATUS_EXIT)
  62. strMsg.Format(_T("ID = %d 退出\n"), m_nThreadID);
  63. else if(m_ThreadStatus == THREADSTATUS_RUNNING)
  64. strMsg.Format(_T("ID = %d 运行\n"), m_nThreadID);
  65. else if(m_ThreadStatus == THREADSTATUS_WAIT)
  66. strMsg.Format(_T("ID = %d 等待\n"), m_nThreadID);
  67. OutputDebugString(strMsg);
  68. #endif //#define _DEBUG
  69. int nIndex = WaitForMultipleObjects(2, m_hEvent, FALSE, INFINITE);
  70. if(nIndex == (WAIT_OBJECT_0 + 1))
  71. {
  72. m_ThreadStatus = THREADSTATUS_END;
  73. return m_ThreadStatus;
  74. }
  75. // 执行事件
  76. m_ThreadStatus = THREADSTATUS_RUNNING;
  77. if(m_lpEvent != NULL)
  78. {
  79. m_lpEvent->eventHandle(m_lpEvent->nEventID, m_lpEvent->pMsg, m_lpEvent->pContext);
  80. m_lpEvent = NULL;
  81. }
  82. // 判断是否超出线程数
  83. m_ThreadStatus = THREADSTATUS_WAIT;
  84. if(m_pThreadPool != NULL)
  85. {
  86. int nRet = m_pThreadPool->IsDeleteThread(this);
  87. if(nRet != 0)
  88. return m_ThreadStatus;
  89. if(nRet == 1)
  90. {
  91. m_ThreadStatus = THREADSTATUS_EXIT;
  92. return m_ThreadStatus;
  93. }
  94. // 转为空闲
  95. m_pThreadPool->MoveToIdleList(this);
  96. m_pThreadPool = NULL;
  97. }
  98. return m_ThreadStatus;
  99. }