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

📄 mythread.cpp

📁 SQLBig5BugTool 宽字符操作问题
💻 CPP
字号:
// **********************************************************************
//
//
// **********************************************************************
#include "stdafx.h"
#include "MyThread.h"

#include <mmsystem.h>
#include <process.h>

#include "mylog.h"
#include "SEHDump.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyThread::CMyThread(IThreadEvent& event)
	:m_event(event)
	,m_hThread(NULL)
	,m_bShouldExit(false)
	,m_bWorkEvent(false)
{
	m_id				= 0;
	m_dwWorkInterval	= 0;
	m_nStatus			= THREADSTATUS_INIT;
	
	m_bShouldExit=false;
	m_bWorkEvent=false;

	m_tThreadStartTime=0;
}

CMyThread::~CMyThread()
{
	if (THREADSTATUS_INIT == m_nStatus)
		return;

	// resume if suspend
	if (THREADSTATUS_SUSPEND == m_nStatus)
		this->Resume();

	// close thread...
	if (THREADSTATUS_CLOSING != m_nStatus)
		this->Close();

	for (int i = 0; i < 2000; i++) {
		if (THREADSTATUS_CLOSED == m_nStatus) {
			break;
		}
	}

	//// waiting for close
	//if (WAIT_TIMEOUT == ::WaitForSingleObject(m_hThread, TIME_WAITINGCLOSE))
	//{
	//	//::TerminateThread(m_hThread, 4444);
	//}

	// release all resources


	if (m_hThread)
	{
		::CloseHandle(m_hThread);
		// m_hThread		= NULL;
	}

	m_id = 0;
}

//////////////////////////////////////////////////////////////////////
bool CMyThread::Close(const DWORD dwWaitMS)
{
	m_bShouldExit=true;

	m_nStatus = THREADSTATUS_CLOSING;

	if(WAIT_OBJECT_0==::WaitForSingleObject(m_hThread,dwWaitMS))
	{
	}
	else
	{
		::TerminateThread(m_hThread, 4444);		
	}

	return true;	
}

//////////////////////////////////////////////////////////////////////
bool 
CMyThread::Init(bool bSuspend/*=true*/, DWORD dwWorkInterval/*=INFINITE*/)
{
	if (THREADSTATUS_INIT != m_nStatus)
		return false;

	// thread
	DWORD dwCreationFlags = 0;
	if (bSuspend)
		dwCreationFlags	= CREATE_SUSPENDED;

	m_bShouldExit=false;
	m_bWorkEvent=false;
/*
	typedef unsigned int	(__stdcall *THREAD_FUNC)(void *);
	m_hThread	= (HANDLE)_beginthreadex (	NULL,			// default security
											0,				// default stack size
											(THREAD_FUNC)RunThread,		// pointer to thread routine
											dwCreationFlags,// start it right away if 0, else suspend
											(unsigned *)&m_id);
*/
	
	m_tThreadStartTime=time(0);
	
	typedef unsigned long	(__stdcall *THREAD_FUNC)(void *);
	m_hThread = ::CreateThread (NULL,						// default security
								0,							// default stack size
								(THREAD_FUNC)CMyThread::RunThread,			// pointer to thread routine
								(void *)this,				// argument for thread
								dwCreationFlags,			// start it right away if 0, else suspend
								(unsigned long*)&m_id);
	
	if (!m_hThread)
	{
		return false;
	}
	m_dwWorkInterval = dwWorkInterval;

	if (bSuspend)
		m_nStatus = THREADSTATUS_SUSPEND;
	else
		m_nStatus = THREADSTATUS_RUNNING;

	return true;
}

//////////////////////////////////////////////////////////////////////
void 
CMyThread::Resume(void)
{
	if (THREADSTATUS_SUSPEND != m_nStatus)
		return;
	
	if (-1 != ::ResumeThread(m_hThread))
	{
		m_tThreadStartTime=time(0);
		
		m_nStatus = THREADSTATUS_RUNNING;
	}
}

//////////////////////////////////////////////////////////////////////
void 
CMyThread::Suspend(void)
{
	if (THREADSTATUS_RUNNING != m_nStatus)
		return;

	if (-1 != ::SuspendThread(m_hThread))
		m_nStatus = THREADSTATUS_SUSPEND;
}


//////////////////////////////////////////////////////////////////////
// static
//////////////////////////////////////////////////////////////////////
CMyThread*	
CMyThread::CreateNew(
					 IThreadEvent& refEvent,
					 bool bSuspend,
					 DWORD dwWorkInterval
					 )
{
	CMyThread* pThread = new CMyThread(refEvent);
	if (!pThread)
		return NULL;

	if (!pThread->Init(bSuspend, dwWorkInterval))
	{
		delete pThread;
		return NULL;
	}

	return pThread;
}

//////////////////////////////////////////////////////////////////////
DWORD WINAPI CMyThread::RunThread(LPVOID pThreadParameter)
{
	InstallXcptProber(true);
	
	CMyThread*	pThread	= (CMyThread*)pThreadParameter;
	if (!pThread)
		return 1;
	
	// init
	
#ifndef _DEBUG
	try
#endif
	{
		if (-1 == pThread->m_event.OnThreadCreate())
			return 2;
	}
#ifndef _DEBUG
	catch (...) 
	{
		WRITE_EXCEPTION_LOG("Exception encountered on calling OnThreadCreate");
	}
#endif
	
	// work now
	while (true)
	{		
		if(pThread->m_bShouldExit)
		{
			// exit event is set
			break;
		}
		if (pThread->m_bWorkEvent)
		{
			pThread->m_bWorkEvent=false;
			
#ifndef _DEBUG
			try
#endif
			{
				// work event is set
				if (-1 == pThread->m_event.OnThreadEvent())
					break;
			}
#ifndef _DEBUG
			catch (...) 
			{
				WRITE_EXCEPTION_LOG("Exception encountered on calling OnThreadEvent");
			}
#endif
			
		}
		// work interval is time out
#ifndef _DEBUG
		try
#endif
		{
			if (-1 == pThread->m_event.OnThreadProcess())
				break;
		}
#ifndef _DEBUG
		catch (...) 
		{
			WRITE_EXCEPTION_LOG("Exception encountered on calling OnThreadProcess");
		}
#endif

		Sleep(5);
	}

	DWORD dwRet = 0;
#ifndef _DEBUG
	try
#endif
	{
		dwRet = pThread->m_event.OnThreadDestroy();

		pThread->m_nStatus = THREADSTATUS_CLOSED;
	}
#ifndef _DEBUG
	catch (...) 
	{
		WRITE_EXCEPTION_LOG("Exception encountered on calling OnThreadDestroy");
	}
#endif

	return dwRet;
}

⌨️ 快捷键说明

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