// CustomThread.h
#ifndef CUSTOMTHREAD_H
#define CUSTOMTHREAD_H
class CCustomThread
{
public:
CCustomThread();
virtual ~CCustomThread();
BOOL Start(LPVOID lpParam);
BOOL End();
virtual void Run();
protected:
static DWORD WINAPI ThreadFun(LPVOID lpParam);
void RunOnceEnd();
DWORD m_dwWaitTimeOut;
BOOL m_bExitThread;
HANDLE m_hTrd;
LPVOID m_lpParam;
};
#endif
// CMyThread
CCustomThread::CCustomThread()
{
m_bExitThread = FALSE;
m_hTrd = NULL;
m_dwWaitTimeOut = 5000;
}
CCustomThread::~CCustomThread()
{
}
BOOL CCustomThread::Start(LPVOID lpParam)
{
m_lpParam = lpParam;
m_bExitThread = FALSE;
m_hTrd = CreateThread(NULL, 0, ThreadFun, this, 0, NULL);
return m_hTrd != NULL;
}
BOOL CCustomThread::End()
{
m_bExitThread = TRUE;
if(m_hTrd != NULL)
{
DWORD dwRet = WaitForSingleObject(m_hTrd, m_dwWaitTimeOut);
if(dwRet != WAIT_OBJECT_0)
{
TerminateThread(m_hTrd, dwRet);
}
CloseHandle(m_hTrd);
m_hTrd = NULL;
}
return TRUE;
}
DWORD WINAPICCustomThread::ThreadFun(LPVOID lpParam)
{
CCustomThread *pTrd = (CCustomThread *)lpParam;
while(!pTrd->m_bExitThread)
{
pTrd->Run();
}
return 0;
}
voidCCustomThread::RunOnceEnd()
{
m_bExitThread = TRUE;
CloseHandle(m_hTrd);
m_hTrd = NULL;
}
voidCCustomThread::Run()
{
}