Thread.cpp
上传用户:chenhai826
上传日期:2007-04-11
资源大小:72k
文件大小:2k
- // Thread.cpp : implementation file of class CThread
- // Created by liangml 2000-7-30
- #include "stdafx.h"
- #include "Thread.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // m_hSomeoneDead is used to signal that one or more threads have ended
- // (it is an auto-reset event; and starts out not signaled)
- HANDLE CThread::m_hSomeoneDead = CreateEvent(NULL, FALSE, FALSE, NULL);
- CThread::CThread(CWnd *pWnd)
- {
- m_bAutoDelete = FALSE;
- m_pMainWnd = pWnd;
- // kill event starts out in the signaled state
- m_hEventKill = CreateEvent(NULL,TRUE,FALSE,NULL);
- m_hEventDead = CreateEvent(NULL,TRUE,FALSE,NULL);
- }
- CThread::~CThread()
- {
- CloseHandle(m_hEventKill);
- CloseHandle(m_hEventDead);
- }
- void CThread::Delete()
- {
- CWinThread::Delete();
- VERIFY(SetEvent(m_hEventDead));
- // VERIFY(SetEvent(m_hSomeoneDead));
- }
- BOOL CThread::InitInstance()
- {
- if(InitWork())
- {
- while(WaitForSingleObject(m_hEventKill,0) == WAIT_TIMEOUT)
- {
- if(Work() == FALSE)
- {
- break;
- }
- }
- CleanupWork();
- }
- // avoid entering standard message loop by returning FALSE
- return FALSE;
- }
- void CThread::KillThread()
- {
- // Note: this function is called in the context of other threads,
- // not the thread itself.
- // reset the m_hEventKill which signals the thread to shutdown
- VERIFY(SetEvent(m_hEventKill));
- // allow thread to run at higher priority during kill process
- SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
- WaitForSingleObject(m_hEventDead, INFINITE);
- WaitForSingleObject(m_hThread, INFINITE);
- // now delete CWinThread object since no longer necessary
- delete this;
- }