www.pudn.com > LECTEUR-MP3.zip > WorkerThread.cpp


// WorkerThread.cpp: implementation of the CWorkerThread class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "MusGest.h" 
#include "WorkerThread.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
//--------------------------------------------------------------------------------- 
CWorkerThread:: CWorkerThread(CView * pParam,CView * pPlayBar, CString * pStrTime) 
{ 
	m_hThread    = 0; 
	m_pParam = pParam; 
	m_pPlayBarView = pPlayBar; 
	m_iWaitTime = 0; 
	m_pstrTime = pStrTime; 
	m_iTime = 0; 
 
	m_WaitThread = CreateEvent(0, TRUE, FALSE, 0); 
} 
 
//--------------------------------------------------------------------------------- 
CWorkerThread::~CWorkerThread() 
{ 
	this->StopThread();		 
} 
 
//--------------------------------------------------------------------------------- 
bool CWorkerThread::InitThread() 
{ 
	m_bStop = false; 
    m_hThread = AfxBeginThread(TabThreadFunc, this); 
       
    if(!m_hThread) 
    { 
        // Impossible de créer le thread ! 
        return false; 
    } 
    return true;                 
} 
 
//--------------------------------------------------------------------------------- 
bool CWorkerThread::StopThread()  
{ 
	if ( this->m_hThread != NULL ) 
	{ 
		//Sort de la pause si il y etait 
		PauseThread(false); 
		// demander de s'arrêter 
		m_bStop = true; 
		// attendre qu'il soit arrêté pour la durée spécifiée 
		if ( ::WaitForSingleObject(*this->m_hThread,1000 ) != WAIT_OBJECT_0 ) 
		{ 
			// échec de l'attente : le thread ne s'est pas terminé 
			// on le tue 
			::TerminateThread( *this->m_hThread, 0 ); 
			this->m_hThread = NULL; 
			OutputDebugString("\nERREUR FIN DE THREAD FORCEE !\t"); 
			return false; 
		} 
		// tout s'est bien passé 
		this->m_hThread = NULL; 
		OutputDebugString("\nFIN NORMALE DU THREAD !\t"); 
	} 
	 
	return true; 
} 
 
//--------------------------------------------------------------------------------- 
void CWorkerThread::PauseThread(bool bWait /*= true*/)  
{ 
	if (bWait) 
	{ 
		if (!m_WaitThread) 
			m_WaitThread = CreateEvent(0, TRUE, FALSE, 0); 
 
		::SetEvent(m_WaitThread); 
	} 
	else 
		m_WaitThread = NULL; 
} 
 
//--------------------------------------------------------------------------------- 
void CWorkerThread::ChangePos(int iModif) 
{ 
	m_iTime += iModif; 
} 
 
 
//--------------------------------------------------------------------------------- 
UINT CWorkerThread::TabThreadFunc(LPVOID pvParam) 
{ 
	CWorkerThread * pThis = reinterpret_cast< CWorkerThread *>( pvParam); 
	CView * pView = pThis->m_pParam; 
	CView * pPlayBar = pThis->m_pPlayBarView; 
	pThis->m_iTime  = 0; 
	bool bMajTime = false; 
	 
	if (pThis && pView && pPlayBar) 
	{ 
		//Permet de ne pas bloquer le thread pendant toute la musique 
		while (pThis->m_bStop == false) 
		{ 
			// attente evenement de fin du thread. -> l'objet doit être ‘signalé ‘ 
			// WaitForSingleObject  renvoie WAIT_OBJECT_0 si l'objet est signalé. 
			if(::WaitForSingleObject(pThis->m_WaitThread, 0) != WAIT_OBJECT_0) 
			{ 
				Sleep(500); 
				pThis->m_iTime += 500; 
 
				//Met a jour toutes les secondes 
				if (bMajTime == true) 
				{ 
					int iMinute = ((pThis->m_iTime / 1000) / 60); 
					int iSecond = ((pThis->m_iTime / 1000) - (iMinute * 60)); 
					pThis->m_pstrTime->Format("%d Min, %d Sec", iMinute,iSecond); 
					pView->PostMessage(WM_MAJ_TIME); 
					pPlayBar->PostMessage(WM_MAJ_TIME); 
					bMajTime = false; 
				} 
				else 
					bMajTime = true; 
 
				if (pThis->m_iTime >= pThis->m_iWaitTime) 
					pThis->m_bStop = true; 
			} 
		} 
 
		//N'envoie le prochain morceau que si celui ci c'est bien terminer 
		if (pThis->m_iTime >= pThis->m_iWaitTime) 
		{ 
			pView->PostMessage(WM_NEXT_FILE); 
			pPlayBar->PostMessage(WM_NEXT_FILE); 
			pThis->m_bStop = true; 
		} 
	} 
 
    return 0; 
}