utiles_v2017/th.cpp

80 lines
2.0 KiB
C++

#include "StdAfx.h"
#include "th.h"
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
//******************************************************
Cth::Cth(void)
{
running =false;
strcpy(nombre, "Proceso sin nombre");
m_thread=NULL;
}
//******************************************************
Cth::~Cth(void)
{
join();
}
//******************************************************
UINT main_th_proceso( LPVOID pp )
{
Cth *p=(Cth*)pp;
p->run();
p->running = false;
AfxEndThread(0,FALSE);
return 0;
}
//******************************************************
bool Cth::isRunning()//indica si se esta currando o no
{
return running;
}
//******************************************************
void Cth::join()//espera al thread
{
if (m_thread == NULL)
return;
WaitForSingleObject(m_thread->m_hThread,INFINITE);
delete m_thread;
m_thread = NULL;
};
//******************************************************
BOOL Cth::lanza(char *nombre)//lanzar thread
{
if (nombre)
strcpy(this->nombre,nombre);
m_thread = AfxBeginThread(main_th_proceso, this, THREAD_PRIORITY_NORMAL, 1024, CREATE_SUSPENDED);
m_thread->m_bAutoDelete=FALSE;
//pone el nombre------------------
#ifdef _DEBUG
SetThName(m_thread->m_nThreadID,this->nombre);
#endif
running = true;
m_thread->ResumeThread();
return TRUE;
};
//******************************************************
UTILES_EXPORT void SetThName( DWORD dwThreadID, char* threadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException( MS_VC_EXCEPTION_TH_UTILES, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
};
//**********************************************************************************************