#pragma once
const DWORD TERMINATION_TIMEOUT = 10000;
class CBaseThread
{
protected:
DWORD m_dwThreadId;
HANDLE m_hTerminateEvent;
HANDLE m_hThread;
protected:
virtual DWORD ThreadProcedure(){return 0;};
public:
bool SetTerminationEvent(HANDLE hTerminationEvent);
bool ShouldTerminateThread();
virtual void TerminateThread();
static DWORD WINAPI ThreadProcedure(PVOID pVoid);
CBaseThread();
virtual ~CBaseThread();
};
2. CBaseThread.cpp
#include "stdafx.h"
#include "CBaseThread.h"
//////////////////////////////
// Construction/Destruction
//////////////////////////////
CBaseThread::CBaseThread()
{
m_dwThreadId = NULL;
m_hTerminateEvent = NULL;
m_hThread = NULL;
m_hTerminateEvent = CreateEvent(NULL,TRUE,FALSE,
ResetEvent(m_hTerminateEvent);
}
CBaseThread::~CBaseThread()
{
CloseHandle(m_hTerminateEvent)
}
//> ------------------------------
//> Function: CBaseThread::ThreadProcedure
//> Params: [in] void *pVoid data passed to the thread procedure
//> Returns: DWORD
//> Purpose: implements the static CBaseThread derived class thread procedure
//> by calling the derived class ThreadProcedure method
//> ------------------------------
DWORD CBaseThread::ThreadProcedure(
{
CBaseThread *pThread = static_cast
return pThread->ThreadProcedure();
}
//> ------------------------------
//> Function: CBaseThread::TerminateThread
//> Params: none
//> Returns: void
//> Purpose: call this method to terminate the thread procedure
//> ------------------------------
void CBaseThread::TerminateThread()
{
// Signal the thread to terminate itself
SetEvent(m_hTerminateEvent);
// wait for the thread procedure to exit before returning
WaitForSingleObject(m_hThread,
if (NULL != m_hThread)
{
CloseHandle(m_hThread);
}
}
//> ------------------------------
//> Function: CBaseThread::
//> Params: void
//> Returns: bool
//> Purpose: check to see if thread termination was requested
//> ------------------------------
bool CBaseThread::
{
bool bReturn = false;
if (WAIT_OBJECT_0 == WaitForSingleObject(m_
{
bReturn = true;
}
return bReturn;
}
3. Usage:
3.1. CDeviceDiscoveryThread.h
#pragma once
#include "cbasethread.h"
#include
#include
#include
#include
class CGigaBluetooth;
class CDeviceDiscoveryThread : public CBaseThread
{
private:
CGigaBluetooth *m_pCMSBluetooth;
protected:
virtual DWORD ThreadProcedure();
public:
static CDeviceDiscoveryThread *ThreadFactory(CGigaBluetooth *pCMSBluetooth);
CDeviceDiscoveryThread(
virtual ~CDeviceDiscoveryThread(void);
};
3.2. CDeviceDiscoveryThread.cpp
#include "StdAfx.h"
#include "GigaBluetooth.h"
#include "DeviceDiscoveryThread.h"
CDeviceDiscoveryThread::
{
ASSERT(NULL != pCMSBluetooth);
m_pCMSBluetooth = pCMSBluetooth;
}
CDeviceDiscoveryThread::~
{
}
//> ------------------------------
//> Function: ThreadFactory
//> Params: MSBluetooth *pMSBluetooth
//> Returns: CDeviceDiscoveryThread *
//> Purpose: creates CDeviceDiscoveryThread threads
//> ------------------------------
CDeviceDiscoveryThread * CDeviceDiscoveryThread::
{
CDeviceDiscoveryThread *pCDeviceDiscoveryThread = new CDeviceDiscoveryThread(
if (NULL != pCDeviceDiscoveryThread)
{
pCDeviceDiscoveryThread->m_
}
return pCDeviceDiscoveryThread;
}
//> ------------------------------
//> Function: ThreadProcedure
//> Params: none
//> Returns: DWORD
//> Purpose: implements the thread procedure
//> ------------------------------
DWORD CDeviceDiscoveryThread::
{
Sleep(5000); //test
CMessages::PostMessage(
return 0;
}
3.3.
CDeviceDiscoveryThread *m_pCDeviceDiscoveryThread;
m_pCDeviceDiscoveryThread = CDeviceDiscoveryThread::
return NULL != m_pCDeviceDiscoveryThread ? true : false;
No comments:
Post a Comment