|
// PSLThreads.cpp : Implementation of CPSLThreads
#include "stdafx.h"
#include "PSLThread.h"
#include "PSLThreads.h"
CPSLThreads::CPSLThreads()
{
}
HRESULT CPSLThreads::OnIndexOutOfRange()
{
return MakeException(exIndexOutOfRange);
}
HRESULT CPSLThreads::FinalConstruct()
{
InternalUpdate();
return S_OK;
}
void CPSLThreads::FinalRelease()
{
}
void CPSLThreads::InternalUpdate()
{
CCritSecLock cs(m_csCollection);
m_coll.clear();
HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if(hSnapshot != INVALID_HANDLE_VALUE)
{
THREADENTRY32 te;
te.dwSize = sizeof(te);
if(::Thread32First(hSnapshot, &te))
{
DWORD dwProcessID = ::GetCurrentProcessId();
do
{
if(te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID) && te.th32OwnerProcessID == dwProcessID)
{
CComObject<CPSLThread> * pThread = NULL;
if(CComObject<CPSLThread>::CreateInstance(&pThread) == S_OK)
{
pThread->Initialize(&te);
m_coll.push_back(CComPtr<IPSLThread>(pThread));
}
}
te.dwSize = sizeof(te);
}
while(::Thread32Next(hSnapshot, &te));
}
::CloseHandle(hSnapshot);
}
}
////////////////////////////////////////////////////////////////////////
// Interface Implementation;
////////////////////////////////////////////////////////////////////////
STDMETHODIMP CPSLThreads::Update()
{
PSL_BEGIN
InternalUpdate();
PSL_END
}
STDMETHODIMP CPSLThreads::Find(long ThreadID, IPSLThread ** ppValue)
{
PSL_BEGIN
*ppValue = NULL;
CCritSecLock cs(m_csCollection);
for(ThreadsType::iterator i = m_coll.begin();i != m_coll.end();i ++)
{
long ID = 0;
if(i->m_T->get_ThreadID(&ID) == S_OK && ID == ThreadID)
{
IPSLThread * pThread = i->m_T;
pThread->AddRef();
*ppValue = pThread;
break;
}
}
PSL_END
}
STDMETHODIMP CPSLThreads::FindCurrent(IPSLThread ** ppValue)
{
PSL_BEGIN
*ppValue = NULL;
CCritSecLock cs(m_csCollection);
for(ThreadsType::iterator i = m_coll.begin();i != m_coll.end();i ++)
{
VARIANT_BOOL IsCurrent = VARIANT_FALSE;
if(i->m_T->get_IsCurrent(&IsCurrent) == S_OK && IsCurrent)
{
IPSLThread * pThread = i->m_T;
pThread->AddRef();
*ppValue = pThread;
break;
}
}
PSL_END
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.