⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customthread.cpp

📁 远程网络监视程序的源码
💻 CPP
字号:
//---------------------------------------------------------------------------
//
// CustomThread.cpp
//
// SUBSYSTEM: 
//              Monitoring process creation and termination  
// MODULE:    
//              Thread management 
//
// DESCRIPTION:
//              This is an abstract class that enables creation of separate 
//              threads of execution in an application. 
//
// AUTHOR:		Ivo Ivanov
//                                                                         
//---------------------------------------------------------------------------
#include "stdafx.h"
#include "CustomThread.h"
#include <process.h>
#include <assert.h>

//---------------------------------------------------------------------------
//
// Thread function prototype
//
//---------------------------------------------------------------------------
typedef unsigned (__stdcall *PTHREAD_START) (void *);

//---------------------------------------------------------------------------
//
// class CCustomThread 
//
// It is an abstract class that enables creation of separate threads of 
// execution.
//                                                                         
//---------------------------------------------------------------------------

CCustomThread::CCustomThread(TCHAR* pszThreadGuid)
	: m_hShutdownEvent(NULL)
	, m_bThreadActive(FALSE)
	, m_hThread(NULL)
	, m_dwThreadId(0)
{
	if (NULL != pszThreadGuid)
		_tcscpy(m_szThreadGuid, pszThreadGuid);
	else
		_tcscpy(m_szThreadGuid, TEXT(""));

	// create shutdown thread event
	if (0 != _tcslen(m_szThreadGuid))
	{
		m_hShutdownEvent = ::CreateEvent(NULL, FALSE, FALSE, m_szThreadGuid);
	}
}

CCustomThread::~CCustomThread()
{	
	SetActive( FALSE );

	if (NULL != m_hShutdownEvent)
	{
		::CloseHandle(m_hShutdownEvent);
	}
}

// Activate / Stop the thread 
BOOL CCustomThread::SetActive(BOOL bValue)
{
	BOOL bCurrent = GetIsActive();
	if ( bValue == bCurrent )
	{
		return TRUE; // already started/stopped
	}

	if ( !(bCurrent || m_hThread) )
	{
		// Perform action prior to activate the thread
		if( !OnBeforeActivate() )
			return FALSE;

		ULONG ulResult = (ULONG)_beginthreadex(
			(void *)NULL,
			(unsigned)0,
			(PTHREAD_START)CCustomThread::ThreadFunc,
			(PVOID)this,
			(unsigned)0,
			(unsigned *)&m_dwThreadId
			);

		if (ulResult == -1L || ulResult == 0)
		{
			return FALSE;
		}
	} 
	else
	{
		if (NULL != m_hShutdownEvent)
		{
			// Signal the thread's event
			::SetEvent(m_hShutdownEvent);
			while( GetIsActive() )
			{
				::Sleep(10);
			}
		}
	}

	return TRUE;
}

// Indicates whether the driver has been activated
BOOL CCustomThread::GetIsActive()
{
	CSingleLock lockMgr(&m_CritSec, TRUE);	
	return m_bThreadActive;
}

//
// Setup the attribute
//
void CCustomThread::SetIsActive(BOOL bValue)
{
	CSingleLock lockMgr(&m_CritSec, TRUE);	
	m_bThreadActive = bValue;

	if( bValue )
	{
		m_hThread = GetCurrentThread();
	}
	else
	{
		m_hThread = NULL;
	}
}


//
// Return the handle to the thread's shut down event
//
HANDLE CCustomThread::Get_ShutdownEvent() const
{
	return m_hShutdownEvent;
}

//
// Primary thread entry point
//
unsigned __stdcall CCustomThread::ThreadFunc(void* pvParam)
{
	CCustomThread* pMe = (CCustomThread*)( pvParam );
	if( !pMe )
	{
		return -1;
	}

	// retrieves a pseudo handle for the current thread
	try
	{
		pMe->SetIsActive( TRUE );

		// Execute the user supplied method
		pMe->Run();
	}
	catch (...)
	{
		// Handle all exceptions
	}

	// do some post transactions
	pMe->OnAfterDeactivate();
	pMe->SetIsActive(FALSE);

	return 0;
}


//
// Perform action prior to activate the thread
//
BOOL CCustomThread::OnBeforeActivate()
{
	// Provide default implementation
	return TRUE;
}

//
// Called after the thread function exits
//
void CCustomThread::OnAfterDeactivate()
{
	// Do nothing
}

//----------------------------End of the file -------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -