BOOL CThread::Start() { if (IsRuning()) return FALSE; m_hThread = (HANDLE)CreateThread(NULL, 0, Runner, (void *)this, 0, &m_dwThreadID); return m_hThread ? TRUE : FALSE; }
//取消配置 bool CQueueServiceThread::UnInitThread() { //效验参数 GT_ASSERT(IsRuning()==false); //设置变量 m_hCompletionPort=NULL; memset(m_cbBuffer,0,sizeof(m_cbBuffer)); return true; }
//配置函数 bool CQueueServiceThread::InitThread(HANDLE hCompletionPort) { //效验参数 ASSERT(IsRuning() == false); ASSERT(m_hCompletionPort == NULL); //设置变量 m_hCompletionPort = hCompletionPort; memset(m_cbBuffer, 0, sizeof(m_cbBuffer)); return true; }
//配置函数 bool CQueueServiceThread::InitThread(HANDLE hCompletionPort,BYTE cbThreadIndex) { //效验参数 GT_ASSERT(IsRuning()==false); GT_ASSERT(m_hCompletionPort==NULL); //设置变量 m_hCompletionPort=hCompletionPort; memset(m_cbBuffer,0,sizeof(m_cbBuffer)); m_cbThreadIndex = cbThreadIndex; return true; }
//启动动画 bool CFlowerEffectThread::StartFlowerEffect(WORD wFlowerID) { //效验参数 ASSERT( wFlowerID < FLOWER_COUNT*2 ); if ( wFlowerID >= FLOWER_COUNT*2 ) return false; m_wFlowerID = wFlowerID; //启动线程 if ( IsRuning() == false ) StartThread(); return true; }
//中止线程 bool CThread::TerminateThread(DWORD dwExitCode) { //停止线程 if (IsRuning()==true) { ::TerminateThread(m_hThreadHandle,dwExitCode); } //设置变量 if (m_hThreadHandle!=NULL) { CloseHandle(m_hThreadHandle); m_hThreadHandle=NULL; m_uThreadID=0; } return true; }
//停止线程 bool CThread::EndThread(DWORD dwWaitSeconds) { //停止线程 if (IsRuning()==true) { m_bRun=false; DWORD dwRetCode=WaitForSingleObject(m_hThreadHandle,dwWaitSeconds); if (dwRetCode==WAIT_TIMEOUT) return false; } //设置变量 if (m_hThreadHandle!=NULL) { CloseHandle(m_hThreadHandle); m_hThreadHandle=NULL; m_uThreadID=0; } return true; }
//启动线程 bool CThread::StartThead() { //效验状态 if (IsRuning()) return false; //清理变量 if (m_hThreadHandle!=NULL) { CloseHandle(m_hThreadHandle); m_hThreadHandle=NULL; m_uThreadID=0; } //构造参数 tagThreadParameter ThreadParameter; ThreadParameter.bSuccess=false; ThreadParameter.m_pServiceThread=this; ThreadParameter.hEventFinish=CreateEvent(NULL,FALSE,FALSE,NULL); if (ThreadParameter.hEventFinish==NULL) return false; //启动线程 m_bRun=true; m_hThreadHandle=(HANDLE)::_beginthreadex(NULL,0,ThreadFunction,&ThreadParameter,0,&m_uThreadID); //等待事件 WaitForSingleObject(ThreadParameter.hEventFinish,INFINITE); //关闭事件 CloseHandle(ThreadParameter.hEventFinish); //判断错误 if (ThreadParameter.bSuccess==false) { EndThread(); return false; } return true; }
BOOL CTransferManager::StopService() { if ( ! IsRuning() ) return TRUE; if ( (ULONG)InterlockedDecrement(&m_nReference) ) return TRUE; m_bThread = FALSE; SetEvent( m_hWakeup ); StopThread(); PostCleanup(); for ( INT_PTR i = 0; i < m_pTransferThreadArray.GetCount(); i++ ) { if ( m_hCompletionPort ) PostQueuedCompletionStatus(m_hCompletionPort,0,NULL,NULL); } for ( i = 0; i < m_pTransferThreadArray.GetCount(); i++ ) { CTransferThread* pTransferThread = m_pTransferThreadArray[ i ]; if ( pTransferThread ) pTransferThread->StopThread(); SAFE_DELETE( pTransferThread ); } m_pTransferThreadArray.RemoveAll(); for ( i = 0; i < m_pTransferArray.GetCount(); i++ ) { m_pTransferArray[ i ]->Release(); } m_pTransferArray.RemoveAll(); if ( m_hCompletionPort != NULL ) { CloseHandle( m_hCompletionPort ); m_hCompletionPort = NULL; } return TRUE; }