这是我正在尝试的代码..
#include "TCHAR.h"
#include "pdh.h"
#include <stdio.h>
#include<iostream>
using namespace std;
static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal;
int _tmain(int argc, _TCHAR* argv[])
{
PdhOpenQuery(NULL, NULL, &cpuQuery);
PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PDH_FMT_COUNTERVALUE counterVal;
PdhCollectQueryData(cpuQuery);
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
cout << counterVal.doubleValue;
getchar();
}
机器详细信息:Windows 8.1 帮助我,它总是显示0或100。
答案 0 :(得分:0)
检索%Processor Time 时,样本集之间必须有延迟
我们需要在PdhCollectQueryData调用之间至少延迟100毫秒。
PdhCollectQueryData(cpuQuery);
this_thread::sleep_for(chrono::milliseconds(1000));
PdhCollectQueryData(cpuQuery);