这个类源代码来源于网上流传的魔域的代码,自己翻来覆去仿写了好多遍都觉得很好用,所以拿出来与大家分享,希望别人什么时候也用得着。
.h
#ifndef THREADBASE_H
#define THREADBASE_H
#include <afxmt.h>
//#include <windows.h>
class CThreadBase
{
protected: // 构造、析构
CThreadBase();
virtual ~CThreadBase();
public: // 公共,由父线程调用。
bool CreateThread(bool bRun = true); // false: 暂不运行,用 ResumeThread() 运行
bool ResumeThread(); // return false: 失败
// 通知子线程关闭,并阻塞 nMilliseconds 毫秒。返回true: 线程关闭成功
bool CloseThread(long nMilliseconds = 0);
/////////////////////////////////////////////////////////////////////
protected: // 派生用
virtual void OnInit();
virtual bool OnProcess(); // 不需要返回DWORD
virtual void OnDestroy();
/////////////////////////////////////////////////////////////////////
private: // 内部使用
bool IsCloseEvent(long nMilliseconds = 0); // 检查关闭线程事件是否触发。用于 ThreadProc() 函数调用
DWORD ThreadProc();
bool IsCreated();
protected:
// CCriticalSection m_xCtrl; // 用于派生类中的变量共享控制
HANDLE m_hThread;
HANDLE m_hCloseThreadEvent;
CCriticalSection m_xCtrl;
/////////////////////////////////////////////////////////////////////
private:
static DWORD WINAPI TrueThreadProc(LPVOID pParam);
};
#endif // THREADBASE_H
.cpp
#include "StdAfx.h"
#include "ThreadBase.h"
CThreadBase::CThreadBase(void)
{
m_hThread = NULL;
m_hCloseThreadEvent = NULL;
}
CThreadBase::~CThreadBase(void)
{
if (NULL != m_hThread)
{
if (::WaitForSingleObject(m_hThread,0) == WAIT_TIMEOUT)
{
::TerminateThread(m_hThread,5);
}
::CloseHandle(m_hThread);
m_hThread = NULL;
}
if (m_hCloseThreadEvent)
{
::CloseHandle(m_hCloseThreadEvent);
m_hCloseThreadEvent = NULL;
}
}
bool CThreadBase::CreateThread(bool bRun /* = true */)
{
if (!IsCreated())
{
m_hCloseThreadEvent = ::CreateEvent(NULL,false,false,NULL);
if (NULL == m_hCloseThreadEvent)
{
return false;
}
DWORD dwThreadID = 0;
DWORD dwCreateFlag = bRun ? 0 : CREATE_SUSPENDED;
m_hThread = ::CreateThread(NULL,0,TrueThreadProc,(void*)this,dwCreateFlag,&dwThreadID);
if (NULL != m_hThread)
{
return true;
}
else
{
::CloseHandle(m_hCloseThreadEvent);
m_hCloseThreadEvent = NULL;
return false;
}
}
return false;
}
bool CThreadBase::ResumeThread()
{
if (NULL != m_hThread)
{
if (::ResumeThread(m_hThread) != -1)
{
return false;
}
else
{
int err = GetLastError();
ASSERT(!err);
return true;
}
}
return false;
}
bool CThreadBase::CloseThread(long nMilliseconds /* = 0 */)
{
if (NULL != m_hThread)
{
::ResumeThread(m_hThread);
::SetEvent(m_hCloseThreadEvent);
if (::WaitForSingleObject(m_hThread,nMilliseconds) != WAIT_TIMEOUT)
{
CloseHandle(m_hThread);
m_hThread = NULL;
CloseHandle(m_hCloseThreadEvent);
m_hCloseThreadEvent = NULL;
return true;
}
else
{
return false;
}
}
return true;
}
bool CThreadBase::IsCreated()
{
return (m_hThread != NULL);
}
void CThreadBase::OnInit()
{
// Do Nothing Here
}
bool CThreadBase::IsCloseEvent(long nMilliseconds /* = 0 */)
{
if (::WaitForSingleObject(m_hCloseThreadEvent,nMilliseconds) != WAIT_TIMEOUT)
{
return true;
}
return false;
}
bool CThreadBase::OnProcess()
{
// Do Nothing here
return false;
}
void CThreadBase::OnDestroy()
{
// Do Nothing Here
}
DWORD CThreadBase::ThreadProc()
{
DWORD bRet = 0;
OnInit();
while (!IsCloseEvent())
{
if (!OnProcess())
{
bRet = 2;
break;
}
}
OnDestroy();
return bRet;
}
DWORD WINAPI CThreadBase::TrueThreadProc(LPVOID pParam)
{
CThreadBase *pThread = (CThreadBase*)(pParam);
return pThread->ThreadProc();
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Minkowsky/archive/2010/06/10/5660150.aspx