Thread详解3:线程的状态及几个简单方法
答案:1 悬赏:40 手机版
解决时间 2021-04-02 14:28
- 提问者网友:心如荒岛囚我终老
- 2021-04-01 22:25
Thread详解3:线程的状态及几个简单方法
最佳答案
- 五星知识达人网友:人類模型
- 2021-04-01 23:15
获得线程的状态的方法
如果是在windows NT下,可以用pdh接口。platform SDK里有pdh.h和pdh.lib,把他们加到项目里来。
下面的代码是我的一个程序的片断:
比如你要查看explorer.exe的第一个线程的状态,可以这样用(当然,你自己要做一些修改):
[cpp] view plaincopy
void func()
{
Initialize();
LONG rt = GetThreadState(_T("explorer/0"));
switch(rt)
{
case 0: //Initialized, The thread is recognized as an object by the microkernel.
break;
case 1: //Ready,The thread is prepared to run on the next free processor.
break;
case 2: //Running, The thread is executing.
break;
case 3: //Standby, The thread is assigned to a processor and about to run. Only one thread can be in Standby state at a time.
break;
case 4: //Terminated, The thread is finished executing.
break;
case 5: //Waiting, The thread is not ready for the processor. When it is ready, it will have to be rescheduled.
break;
case 6: //Transition, The thread is ready but waiting for resources other than the processor to become available.
break;
case 7: //unknown, The thread state is unknown.
break;
default:
break;
}
}
BOOL CPdhWrapper::Initialize()
{
typedef long (__stdcall *PDHOpenQuery) (LPCSTR, DWORD_PTR, HQUERY);
PDHOpenQuery pfnPdhOpenQuery;
m_hPdhModule = LoadLibrary(_T("PDH.DLL"));
if(m_hPdhModule == NULL)
return FALSE;
// 这个地方ms弄错了,pdh.h里声明的是PdhOpenQueryA,但pdh.dll里导出的却是PdhOpenQuery。
pfnPdhOpenQuery = (PDHOpenQuery)GetProcAddress(m_hPdhModule, "PdhOpenQuery");
if(pfnPdhOpenQuery == NULL)
return FALSE;
return ERROR_SUCCESS == pfnPdhOpenQuery(NULL, 1, &m_hQuery);
}
LONG CPdhWrapper::QueryData(LPCTSTR lpCounterName)
{
HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE pdhFormattedValue;
if(ERROR_SUCCESS != PdhAddCounter(m_hQuery, lpCounterName, NULL, &hCounter))
return -1;
if(ERROR_SUCCESS != PdhCollectQueryData(m_hQuery)
|| ERROR_SUCCESS != PdhGetFormattedCounterValue(
hCounter,
PDH_FMT_LONG,
NULL,
&pdhFormattedValue )
|| ERROR_SUCCESS != pdhFormattedValue.CStatus)
{
pdhFormattedValue.longValue = -1;
}
PdhRemoveCounter(hCounter);
return pdhFormattedValue.longValue;
}
DWORD CPdhWrapper::GetThreadStat(LPCTSTR szThreadName)
{
TCHAR szCounterName[256];
_stprintf(szCounterName, _T("//Thread(%s)//Thread State"),szThreadName);
return QueryData(szCounterName);
}
如果是在windows NT下,可以用pdh接口。platform SDK里有pdh.h和pdh.lib,把他们加到项目里来。
下面的代码是我的一个程序的片断:
比如你要查看explorer.exe的第一个线程的状态,可以这样用(当然,你自己要做一些修改):
[cpp] view plaincopy
void func()
{
Initialize();
LONG rt = GetThreadState(_T("explorer/0"));
switch(rt)
{
case 0: //Initialized, The thread is recognized as an object by the microkernel.
break;
case 1: //Ready,The thread is prepared to run on the next free processor.
break;
case 2: //Running, The thread is executing.
break;
case 3: //Standby, The thread is assigned to a processor and about to run. Only one thread can be in Standby state at a time.
break;
case 4: //Terminated, The thread is finished executing.
break;
case 5: //Waiting, The thread is not ready for the processor. When it is ready, it will have to be rescheduled.
break;
case 6: //Transition, The thread is ready but waiting for resources other than the processor to become available.
break;
case 7: //unknown, The thread state is unknown.
break;
default:
break;
}
}
BOOL CPdhWrapper::Initialize()
{
typedef long (__stdcall *PDHOpenQuery) (LPCSTR, DWORD_PTR, HQUERY);
PDHOpenQuery pfnPdhOpenQuery;
m_hPdhModule = LoadLibrary(_T("PDH.DLL"));
if(m_hPdhModule == NULL)
return FALSE;
// 这个地方ms弄错了,pdh.h里声明的是PdhOpenQueryA,但pdh.dll里导出的却是PdhOpenQuery。
pfnPdhOpenQuery = (PDHOpenQuery)GetProcAddress(m_hPdhModule, "PdhOpenQuery");
if(pfnPdhOpenQuery == NULL)
return FALSE;
return ERROR_SUCCESS == pfnPdhOpenQuery(NULL, 1, &m_hQuery);
}
LONG CPdhWrapper::QueryData(LPCTSTR lpCounterName)
{
HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE pdhFormattedValue;
if(ERROR_SUCCESS != PdhAddCounter(m_hQuery, lpCounterName, NULL, &hCounter))
return -1;
if(ERROR_SUCCESS != PdhCollectQueryData(m_hQuery)
|| ERROR_SUCCESS != PdhGetFormattedCounterValue(
hCounter,
PDH_FMT_LONG,
NULL,
&pdhFormattedValue )
|| ERROR_SUCCESS != pdhFormattedValue.CStatus)
{
pdhFormattedValue.longValue = -1;
}
PdhRemoveCounter(hCounter);
return pdhFormattedValue.longValue;
}
DWORD CPdhWrapper::GetThreadStat(LPCTSTR szThreadName)
{
TCHAR szCounterName[256];
_stprintf(szCounterName, _T("//Thread(%s)//Thread State"),szThreadName);
return QueryData(szCounterName);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯