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

📄 multithread.cpp

📁 PPC开发的代码
💻 CPP
字号:
// multiThrd.cpp : implementation of the worker thread
#include "stdafx.h"
#include "multiThread.h"
//引入用户定义的标准消息
#include "userMessage.h"

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

// 1999-05-01 ChK
// 在新代码中已不再用CMultiThreadBase
// 改用CThreadInfo来辅助CWinThread完成多线程
CThreadInfo::CThreadInfo()
{
	m_isRuning = FALSE;
	m_isAborting = FALSE;
	m_hwndNotify = NULL;
	m_pEVIsIdle = new CEvent( FALSE, TRUE );
	m_pEVIsIdle->SetEvent();
}

CThreadInfo::~CThreadInfo()
{
	if( m_pEVIsIdle != NULL )
	{
		delete m_pEVIsIdle;
	}
}

void CThreadInfo::Progress( WORD p1, DWORD p2 )
{
	if( m_hwndNotify == NULL )
	{
		return;
	}

	if( m_oldP1 == p1 && m_oldP2 == p2 )
	{
		return;
	}

	::PostMessage( m_hwndNotify, WM_USER_THREAD_IN_PROGRESS, p1, p2 );
	m_oldP1 = p1;
	m_oldP2 = p2;
}

void CThreadInfo::ThreadExit( )
{
	if( m_hwndNotify == NULL )
	{
		return;
	}
	::PostMessage( m_hwndNotify, WM_USER_THREAD_EXIT, 0, 0L );

	m_isRuning = FALSE;
	m_pEVIsIdle->SetEvent();
}

void CThreadInfo::ThreadDone( )
{
	if( m_hwndNotify == NULL )
	{
		return;
	}
	::PostMessage( m_hwndNotify, WM_USER_THREAD_DONE, 0, 0L );
	
	m_isRuning = FALSE;
	m_pEVIsIdle->SetEvent();
}

void CThreadInfo::ThreadStart()
{
	m_pEVIsIdle->ResetEvent();
	m_isRuning = TRUE;
	SetAborting( FALSE );
}


/////////////////////////////////////////////////////////////////////////////
// CWinThreadEx

IMPLEMENT_DYNCREATE(CWinThreadEx, CWinThread)

CWinThreadEx::CWinThreadEx()
{
	m_bAutoDelete = TRUE;
}

CWinThreadEx::~CWinThreadEx()
{
}

BOOL CWinThreadEx::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CWinThreadEx::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CWinThreadEx, CWinThread)
	//{{AFX_MSG_MAP(CWinThreadEx)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
	ON_THREAD_MESSAGE(WM_USER_THREAD_START, OnStartProcess)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinThreadEx message handlers
BOOL CWinThreadEx::StartProcess()
{
	if( CanStartProcess() )
	{
		return PostThreadMessage( WM_USER_THREAD_START, 0, 0 );
	}
	return FALSE;
}

void CWinThreadEx::Process()
{//子类可以在这里进行任务处理
}

BOOL CWinThreadEx::CanStartProcess()
{
	return (!m_isRuning && m_hwndNotify != NULL);
}

void CWinThreadEx::OnStartProcess( UINT wParam, LONG lParam )
{
	ThreadStart();
	Process();
	ThreadDone( );
}

void CWinThreadEx::Quit()
{
	//结束过程
	StopProcess();
	//通知线程结束
	PostThreadMessage( WM_QUIT, 0, 0 );
	//等结束标志
	WaitForSingleObject( *this, INFINITE );
}

BOOL CWinThreadEx::StopProcess()
{
	if( !m_isRuning )
	{
		return TRUE;
	}
	SetAborting( TRUE );
	//等处理过程结束
	WaitForSingleObject( *m_pEVIsIdle, INFINITE );
	return TRUE;
}

⌨️ 快捷键说明

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