#include "StdAfx.h" #include "ThreadControl.h" CThreadControl::CThreadControl(void) { m_nWorkingThread= 0; m_nThreadLoop= 0; m_pThread= NULL; m_pThreadWorker= NULL; InitializeCriticalSection(&m_csWorkingEnd); } CThreadControl::~CThreadControl(void) { ReleaseThreadControl(); DeleteCriticalSection(&m_csWorkingEnd); } int CThreadControl::StartThreadControl() { if (m_pThread) { for (int i = 0; i < m_nThreadLoop; i++) { m_pThread[i].StartThreadWork(); } } return m_nThreadLoop; } void CThreadControl::StopThreadControl() { if (m_pThread) { for (int i = 0; i < m_nThreadLoop; i++) { m_pThread[i].StopThread(); } } } BOOL CThreadControl::InitThreadControl(int nThread, IThreadWorker* pThreadWorker) { if(nThread == m_nThreadLoop) return TRUE; ReleaseThreadControl(); if(nThread < 1) nThread= 1; if(nThread > MAX_THREAD) nThread= MAX_THREAD; m_pThreadWorker = pThreadWorker; m_pThread = new CInspectThread[nThread]; m_nThreadLoop = 0; for (int i = 0; i < nThread; i++) { m_pThread[i].SetWorker(static_cast(this)); m_pThread[i].StartThreadLoop(i); m_nThreadLoop++; } return TRUE; } BOOL CThreadControl::ReleaseThreadControl() { if (m_pThread && m_nThreadLoop > 0) { int i; for (i = 0; i < m_nThreadLoop; i++) { m_pThread[i].StopThread(); m_pThread[i].WaitThreadEnd(); } Sleep(500); for (i = 0; i < m_nThreadLoop; i++) { if (m_pThread[i].IsRunning()) { m_pThread[i].SuspendThread(); Sleep(10); TerminateThread(m_pThread[i].m_hThread, 0); g_pLog->DisplayMessage(_T("Deinit :: Terminate Thread : %d"), i); } } m_nThreadLoop = 0; delete [] m_pThread; m_pThread = NULL; } return TRUE; } BOOL CThreadControl::OnThreadRun(int iThread, CInspectThread *pInspectThread) { EnterCriticalSection(&m_csWorkingEnd); m_nWorkingThread++; LeaveCriticalSection(&m_csWorkingEnd); Sleep(1);// 窍绰 老捞 绝栏搁 OnThreadEndAll 捞 静饭靛 俺荐 父怒 龋免瞪 荐 乐促. if(m_pThreadWorker) return m_pThreadWorker->OnThreadRun(iThread, pInspectThread); return TRUE; } BOOL CThreadControl::OnThreadEnd(int iThread, CInspectThread *pInspectThread) { BOOL bRet= FALSE; if(m_pThreadWorker) m_pThreadWorker->OnThreadEnd(iThread, pInspectThread); EnterCriticalSection(&m_csWorkingEnd); m_nWorkingThread--; if(m_nWorkingThread == 0) { bRet= OnThreadEndAll(); } LeaveCriticalSection(&m_csWorkingEnd); return bRet;//m_nWorkingThread == 0; } BOOL CThreadControl::OnThreadEndAll() { BOOL bRet= TRUE; if(m_pThreadWorker) { bRet= m_pThreadWorker->OnThreadEndAll(); } return bRet; } IMPLEMENT_DYNCREATE(CInspectThread, CWinThread) CInspectThread::~CInspectThread() { DeleteCriticalSection(&m_csExit); } CInspectThread::CInspectThread() { m_bRunning = FALSE; m_bThreadRunning = FALSE; m_bWorking = FALSE; m_iThread = -1; m_iCamera = -1; // m_nFrameDefBlobLimit = 0; m_pWorker= NULL; InitializeCriticalSection(&m_csExit); } BEGIN_MESSAGE_MAP(CInspectThread, CWinThread) //{{AFX_MSG_MAP(CInspectionThread) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CInspectThread::StartThreadLoop(int iThread) { if (m_bRunning) return FALSE; m_iThread = iThread; EnterCriticalSection(&m_csExit); m_bRunning = TRUE; LeaveCriticalSection(&m_csExit); CreateThread(); return TRUE; } void CInspectThread::StopThread() { EnterCriticalSection(&m_csExit); m_bRunning = FALSE; LeaveCriticalSection(&m_csExit); } BOOL CInspectThread::WaitThreadEnd(int ms) { int count= 0; while (m_bThreadRunning) { Sleep(1); count++; if(ms > 0) // 措扁矫埃 力茄捞 乐栏搁. { if(count > ms) return FALSE; // 措扁矫埃 悼救 沥惑 辆丰 给窍绊 府畔. } } return TRUE; // 沥惑 辆丰 犬牢. } BOOL CInspectThread::InitInstance() { // TODO: perform and per-thread initialization here return TRUE; } int CInspectThread::ExitInstance() { // TODO: perform any per-thread cleanup here return CWinThread::ExitInstance(); } int CInspectThread::Run() { m_bThreadRunning= TRUE; while(m_bRunning && m_pWorker) { if(m_bWorking) { m_pWorker->OnThreadRun(m_iThread, this); m_pWorker->OnThreadEnd(m_iThread, this); m_bWorking= FALSE; }else { Sleep(1); } } ExitInstance(); m_bThreadRunning= FALSE; return CWinThread::Run(); }