123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include "stdafx.h"
- #include "WorkThread.h"
- #include <windows.h>
- #include "msgdef.h"
- #include "ThreadPool.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CWorkThread::CWorkThread()
- {
- m_lpEvent = NULL;
- m_pThreadPool = NULL;
- }
- CWorkThread::~CWorkThread()
- {
- m_lpEvent = NULL;
- m_pThreadPool = NULL;
- }
- /************************************************************************/
- /*
- 函数:AddEvent
- 描述:添加事项
- 参数:
- IN CONST LPSEVENT lpEvent, 工作事项
- IN CONST CThreadPool* p 线程池
- 返回:
- */
- /************************************************************************/
- int CWorkThread::AddEventItem(IN CONST LPSEVENT lpEvent, IN CONST CThreadPool* p)
- {
- if(lpEvent == NULL || p == NULL)
- return 0;
- m_lpEvent = lpEvent;
- m_lpEvent->nEventID = m_nThreadID;
- m_pThreadPool = (CThreadPool*)p;
- return 1;
- }
- /************************************************************************/
- /*
- 函数:Run
- 描述:运行
- 参数:
- 返回:1成功,0失败
- */
- /************************************************************************/
- THREADSTATUS CWorkThread::Run()
- {
- /*
- WaitForSingleObject(m_hWakeUpSignal, INFINITE);
- // 等待唤醒事件
- if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
- {
- m_ThreadStatus = THREADSTATUS_EXIT;
- return m_ThreadStatus;
- }
- */
- #ifdef _DEBUG
- CString strMsg = _T("");
- if(m_ThreadStatus == THREADSTATUS_EXIT)
- strMsg.Format(_T("ID = %d 退出\n"), m_nThreadID);
- else if(m_ThreadStatus == THREADSTATUS_RUNNING)
- strMsg.Format(_T("ID = %d 运行\n"), m_nThreadID);
- else if(m_ThreadStatus == THREADSTATUS_WAIT)
- strMsg.Format(_T("ID = %d 等待\n"), m_nThreadID);
- OutputDebugString(strMsg);
- #endif //#define _DEBUG
- int nIndex = WaitForMultipleObjects(2, m_hEvent, FALSE, INFINITE);
- if(nIndex == (WAIT_OBJECT_0 + 1))
- {
- m_ThreadStatus = THREADSTATUS_END;
- return m_ThreadStatus;
- }
- // 执行事件
- m_ThreadStatus = THREADSTATUS_RUNNING;
- if(m_lpEvent != NULL)
- {
- m_lpEvent->eventHandle(m_lpEvent->nEventID, m_lpEvent->pMsg, m_lpEvent->pContext);
- m_lpEvent = NULL;
- }
- // 判断是否超出线程数
- m_ThreadStatus = THREADSTATUS_WAIT;
- if(m_pThreadPool != NULL)
- {
- int nRet = m_pThreadPool->IsDeleteThread(this);
- if(nRet != 0)
- return m_ThreadStatus;
- if(nRet == 1)
- {
- m_ThreadStatus = THREADSTATUS_EXIT;
- return m_ThreadStatus;
- }
- // 转为空闲
- m_pThreadPool->MoveToIdleList(this);
- m_pThreadPool = NULL;
- }
- return m_ThreadStatus;
- }
|