<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class
CThread
{
public:
/**//**
*DefaultConstructor
*/
CThread()
{
m_pThreadFunction=CThread::EntryPoint;
m_runthread=FALSE;
}
/**//**
*DefaultDestructor
*alsodestroysthethread
*/
~CThread()
{
if(m_hThread)
Stop(true);//threadstillrunning,soforcethethreadtostop!
}
/**//**
*Startsthethread.
*@paramdwCreationFlagstheflagstouseforcreatingthethread.seeCreateThread()inthewindowssdk.
*/
DWORDStart(DWORDdwCreationFlags=0)
{
m_runthread=true;
m_hThread=CreateThread(NULL,0,m_pThreadFunction,this,0,&dwCreationFlags);
m_dwExitCode=(DWORD)-1;
returnGetLastError();
}
/**//**
*Stopsthethread.
*
*@parambForceKilliftrue,theThreadiskilledimmediately
*/
DWORDStop(boolbForceKill=false)
{
if(m_hThread)
{
//尝试"温柔地"结束线程
if(m_runthread==TRUE)
m_runthread=FALSE;//first,trytostopthethreadnice
GetExitCodeThread(m_hThread,&m_dwExitCode);
if(m_dwExitCode==STILL_ACTIVE&&bForceKill)
{//强制杀死线程
TerminateThread(m_hThread,DWORD(-1));
m_hThread=NULL;
}
}
returnm_dwExitCode;
}
/**//**
*Stopsthethread.firsttellthethreadtostopitselfandwaitforthethreadtostopitself.
*iftimeoutoccursandthethreadhasn'tstoppedyet,thenthethreadiskilled.
*@paramtimeoutmillisecondstowaitforthethreadtostopitself
*/
DWORDStop(WORDtimeout)
{
Stop(false);
WaitForSingleObject(m_hThread,timeout);//等待一段时间
returnStop(true);
}
/**//**
*suspendsthethread.i.e.thethreadishaltedbutnotkilled.TostartasuspendedthreadcallResume().
*/
DWORDSuspend()
{//挂起线程
returnSuspendThread(m_hThread);
}
/**//**
*resumesthethread.thismethodstartsacreatedandsuspendedthreadagain.
*/
DWORDResume()
{//恢复线程
returnResumeThread(m_hThread);
}
/**//**
*setsthepriorityofthethread.
*@paramprioritythepriority.seeSetThreadPriority()inwindowssdkforpossiblevalues.
*@returntrueifsuccessful
*/
BOOLSetPriority(intpriority)
{//设置线程优先级
returnSetThreadPriority(m_hThread,priority);
}
/**//**
*getsthecurrentpriorityvalueofthethread.
*@returnthecurrentpriorityvalue
*/
intGetPriority()
{//获取线程优先级
returnGetThreadPriority(m_hThread);
}
protected:
/**//**
*子类应该重写此方法,这个方法是实际的工作线程函数
*/
virtualDWORDThreadMethod()=0;
private:
/**//**
*DONToverridethismethod.
*
*thismethodisthe"function"usedwhencreatingthethread.itisstaticsothatway
*apointertoitisavailableinsidetheclass.thismethodcallsthenthevirtual
*methodoftheparentclass.
*/
staticDWORDWINAPIEntryPoint(LPVOIDpArg)
{
CThread*pParent=reinterpret_cast<CThread*>(pArg);
pParent->ThreadMethod();//多态性,调用子类的实际工作函数
return0;
}
private:
HANDLEm_hThread;/**////<ThreadHandle线程句柄
DWORDm_dwTID;///<ThreadID线程ID
LPVOIDm_pParent;///<thispointeroftheparentCThreadobject
DWORDm_dwExitCode;///<ExitCodeofthethread线程退出码
protected:
LPTHREAD_START_ROUTINEm_pThreadFunction;/**////<工作线程指针
BOOLm_runthread;///<线程是否继续运行的标志
};
{
public:
/**//**
*DefaultConstructor
*/
CThread()
{
m_pThreadFunction=CThread::EntryPoint;
m_runthread=FALSE;
}
/**//**
*DefaultDestructor
*alsodestroysthethread
*/
~CThread()
{
if(m_hThread)
Stop(true);//threadstillrunning,soforcethethreadtostop!
}
/**//**
*Startsthethread.
*@paramdwCreationFlagstheflagstouseforcreatingthethread.seeCreateThread()inthewindowssdk.
*/
DWORDStart(DWORDdwCreationFlags=0)
{
m_runthread=true;
m_hThread=CreateThread(NULL,0,m_pThreadFunction,this,0,&dwCreationFlags);
m_dwExitCode=(DWORD)-1;
returnGetLastError();
}
/**//**
*Stopsthethread.
*
*@parambForceKilliftrue,theThreadiskilledimmediately
*/
DWORDStop(boolbForceKill=false)
{
if(m_hThread)
{
//尝试"温柔地"结束线程
if(m_runthread==TRUE)
m_runthread=FALSE;//first,trytostopthethreadnice
GetExitCodeThread(m_hThread,&m_dwExitCode);
if(m_dwExitCode==STILL_ACTIVE&&bForceKill)
{//强制杀死线程
TerminateThread(m_hThread,DWORD(-1));
m_hThread=NULL;
}
}
returnm_dwExitCode;
}
/**//**
*Stopsthethread.firsttellthethreadtostopitselfandwaitforthethreadtostopitself.
*iftimeoutoccursandthethreadhasn'tstoppedyet,thenthethreadiskilled.
*@paramtimeoutmillisecondstowaitforthethreadtostopitself
*/
DWORDStop(WORDtimeout)
{
Stop(false);
WaitForSingleObject(m_hThread,timeout);//等待一段时间
returnStop(true);
}
/**//**
*suspendsthethread.i.e.thethreadishaltedbutnotkilled.TostartasuspendedthreadcallResume().
*/
DWORDSuspend()
{//挂起线程
returnSuspendThread(m_hThread);
}
/**//**
*resumesthethread.thismethodstartsacreatedandsuspendedthreadagain.
*/
DWORDResume()
{//恢复线程
returnResumeThread(m_hThread);
}
/**//**
*setsthepriorityofthethread.
*@paramprioritythepriority.seeSetThreadPriority()inwindowssdkforpossiblevalues.
*@returntrueifsuccessful
*/
BOOLSetPriority(intpriority)
{//设置线程优先级
returnSetThreadPriority(m_hThread,priority);
}
/**//**
*getsthecurrentpriorityvalueofthethread.
*@returnthecurrentpriorityvalue
*/
intGetPriority()
{//获取线程优先级
returnGetThreadPriority(m_hThread);
}
protected:
/**//**
*子类应该重写此方法,这个方法是实际的工作线程函数
*/
virtualDWORDThreadMethod()=0;
private:
/**//**
*DONToverridethismethod.
*
*thismethodisthe"function"usedwhencreatingthethread.itisstaticsothatway
*apointertoitisavailableinsidetheclass.thismethodcallsthenthevirtual
*methodoftheparentclass.
*/
staticDWORDWINAPIEntryPoint(LPVOIDpArg)
{
CThread*pParent=reinterpret_cast<CThread*>(pArg);
pParent->ThreadMethod();//多态性,调用子类的实际工作函数
return0;
}
private:
HANDLEm_hThread;/**////<ThreadHandle线程句柄
DWORDm_dwTID;///<ThreadID线程ID
LPVOIDm_pParent;///<thispointeroftheparentCThreadobject
DWORDm_dwExitCode;///<ExitCodeofthethread线程退出码
protected:
LPTHREAD_START_ROUTINEm_pThreadFunction;/**////<工作线程指针
BOOLm_runthread;///<线程是否继续运行的标志
};
子类只需要从此类继承并重写ThreadMethod()方法就可以了。