?? multithread.cpp
字號:
// multiThrd.cpp : implementation of the worker thread
#include "stdafx.h"
#include "multiThread.h"
//引入用戶定義的標(biāo)準(zhǔn)消息
#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)
{
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()
{//子類可以在這里進行任務(wù)處理
}
BOOL CWinThreadEx::CanStartProcess()
{
return (!m_isRuning && m_hwndNotify != NULL);
}
void CWinThreadEx::OnStartProcess( UINT wParam, LONG lParam )
{
ThreadStart();
Process();
ThreadDone( );
}
void CWinThreadEx::Quit()
{
//結(jié)束過程
StopProcess();
//通知線程結(jié)束
PostThreadMessage( WM_QUIT, 0, 0 );
//等結(jié)束標(biāo)志
WaitForSingleObject( *this, INFINITE );
}
BOOL CWinThreadEx::StopProcess()
{
if( !m_isRuning )
{
return TRUE;
}
SetAborting( TRUE );
//等處理過程結(jié)束
WaitForSingleObject( *m_pEVIsIdle, INFINITE );
return TRUE;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -