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

📄 threadjob.cpp

📁 RTMS设备通讯协议
💻 CPP
字号:
// ThreadJob.cpp: implementation of the CThreadJob class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RTMS.h"
#include "ThreadJob.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CThreadJob, CObject)

CThreadJob::CThreadJob()
{
	m_pThread = NULL;
}

CThreadJob::~CThreadJob()
{
	if (m_pThread) {
		TRACE("CThreadJob: *** Warning: deleting active thread! ***\n");
		delete m_pThread;
	}
}

UINT CThreadJob::ThreadProc(LPVOID pObj)
{
	CThreadJob* pJob = (CThreadJob*)pObj;
	ASSERT_KINDOF(CThreadJob, pJob);
	pJob->m_uErr = pJob->DoWork();		 // call virt fn to do the work  
	return pJob->m_uErr;						 // ..and return error code to Windows
}

BOOL CThreadJob::Begin(CWnd* pWndOwner, UINT ucbMsg)
{
	m_hWndOwner = pWndOwner->GetSafeHwnd();
	m_ucbMsg = ucbMsg;
	m_bAbort = FALSE;
	m_uErr = 0;
	m_pThread = AfxBeginThread(ThreadProc, 
		(LPVOID)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	ASSERT(m_pThread);
	m_pThread->m_bAutoDelete = FALSE;
	m_pThread->ResumeThread();
	return m_pThread != NULL;
}

void CThreadJob::Abort(BOOL bAbort)
{
	m_bAbort = bAbort;
}


void CThreadJob::OnProgress(WPARAM wp, LPARAM lp)
{
	if (m_hWndOwner && m_ucbMsg)
		::PostMessage(m_hWndOwner, m_ucbMsg, wp, lp);
}

BOOL CThreadJob::IsNormalQuit()
{
	int rc;
	try
	{   
		rc = ::WaitForSingleObject(m_pThread->m_hThread,0);
		if( WAIT_OBJECT_0 == rc )
		{
			delete m_pThread;
			m_pThread = NULL;
			return TRUE; //线程正常结束
		}
	}
	catch(...){return TRUE;} //线程异常结束
	return FALSE; //线程未结束
}

⌨️ 快捷键说明

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