基本信息
源码名称:多线程控制
源码大小:1.49KB
文件格式:.cpp
开发语言:C/C++
更新时间:2015-06-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
#include "StdAfx.h" #include "SubThread.h" CSubThread::CSubThread(void) { m_pThreadFunction = CSubThread::EntryPoint; m_runthread = FALSE; } CSubThread::~CSubThread(void) { if ( m_hThread ) Stop(true); } DWORD CSubThread::Start(DWORD dwCreationFlags = 0) { m_runthread = true; m_hThread = CreateThread(NULL, 0, m_pThreadFunction, this, dwCreationFlags,&m_dwTID); m_dwExitCode = (DWORD)-1; return GetLastError(); } DWORD CSubThread::Stop ( bool bForceKill = false ) { if ( m_hThread ) { //尝试"温柔地"结束线程 if (m_runthread == TRUE) m_runthread = FALSE; //first, try to stop the thread nice GetExitCodeThread(m_hThread, &m_dwExitCode); if ( m_dwExitCode == STILL_ACTIVE && bForceKill ) { //强制杀死线程 TerminateThread(m_hThread, DWORD(-1)); m_hThread = NULL; } } return m_dwExitCode; } DWORD CSubThread::Stop ( WORD timeout ) { Stop(false); WaitForSingleObject(m_hThread, timeout);//等待一段时间 return Stop(true); } DWORD CSubThread::Suspend() { return SuspendThread(m_hThread); } DWORD CSubThread::Resume() { return ResumeThread(m_hThread); } BOOL CSubThread::SetPriority(int priority) { return SetThreadPriority(m_hThread, priority); } int CSubThread::GetPriority() { return GetThreadPriority(m_hThread); } DWORD WINAPI CSubThread::EntryPoint( LPVOID pArg) { CSubThread *pParent = reinterpret_cast<CSubThread*>(pArg); pParent->ThreadMethod();//多态性,调用子类的实际工作函数 return 0; }