Click here to Skip to main content
16,008,075 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: View calling a child frame Pin
Christian Graus23-Oct-01 10:36
protectorChristian Graus23-Oct-01 10:36 
GeneralRe: View calling a child frame Pin
jangel24-Oct-01 12:21
jangel24-Oct-01 12:21 
GeneralRe: View calling a child frame Pin
Christian Graus24-Oct-01 12:27
protectorChristian Graus24-Oct-01 12:27 
GeneralRe: View calling a child frame Pin
jangel24-Oct-01 12:35
jangel24-Oct-01 12:35 
QuestionTask Manager process CPU usage? Pin
Peter Sjöström22-Oct-01 13:29
Peter Sjöström22-Oct-01 13:29 
AnswerRe: Task Manager process CPU usage? Pin
Bill Wilson22-Oct-01 13:41
Bill Wilson22-Oct-01 13:41 
GeneralRe: Task Manager process CPU usage? Pin
Peter Sjöström23-Oct-01 1:59
Peter Sjöström23-Oct-01 1:59 
GeneralRe: Task Manager process CPU usage? Pin
Bill Wilson23-Oct-01 7:37
Bill Wilson23-Oct-01 7:37 
I don't know which method your asking about...

With WMI, get all instances of Win_32_Processor provider and query the LoadPercentage. This will return an average over 1 second.

Here is the class I ended up using in my project utilizing the PDH.dll. I create an instance of the class for each cpu on the system, query each and take an average, when I want to know the answer.

Class definition:

#ifndef __CPUUSAGE_CLASS__
#define __CPUUSAGE_CLASS__
#include<pdh.h>



class CCPUUsage {     
public: 
  CCPUUsage();
  ~CCPUUsage();
#pragma message("CCPUUsage")
	BOOL Init(LONG nMinTicksBetweenSamples = 1000, int iProcessor = 0);

	int GetUsage(PBOOL pfNewSample = NULL);

private: 
  HQUERY m_hQuery;
  HCOUNTER m_hCounter;
  int m_nMinTicksBetweenSamples;
  DWORD m_msLastTick;
  int m_nLastSampledCPUUsage;
};


#endif // __CPUUSAGE_CLASS__



Class implementation:


CCPUUsage::CCPUUsage()
{


}


CCPUUsage::~CCPUUsage()
{
PdhCloseQuery(m_hQuery);

}

BOOL CCPUUsage::Init(LONG nMinTicksBetweenSamples, int iProcessor)
{
m_nMinTicksBetweenSamples = nMinTicksBetweenSamples;
m_msLastTick = 0;
// m_msLastTick = GetTickCount();

PDH_STATUS pdhs = PdhOpenQuery(NULL, 0, &m_hQuery);
CString csPath;
csPath.Format("\\Processor(%d)\\%% processor time", iProcessor);
pdhs = PdhAddCounter(m_hQuery,
TEXT(csPath), 0, &m_hCounter);

CString strDiag;
strDiag.Format("CCPUUsage::Init, pdhs = %x, Processor = %d",pdhs,iProcessor);
TRACEX(strDiag);
m_nLastSampledCPUUsage = 0;
return (pdhs == 0);

}

int CCPUUsage::GetUsage(PBOOL pfNewSample ) {

BOOL fNewSample;
if (pfNewSample == NULL)
pfNewSample = &fNewSample;

*pfNewSample = FALSE;

DWORD msCurrentTick = GetTickCount();
DWORD msLastTick = m_msLastTick;
if (msCurrentTick < msLastTick) {
// Wrap around, always sample
*pfNewSample = TRUE;
} else {
if ((msLastTick + m_nMinTicksBetweenSamples) < msCurrentTick) {
// It's been long since the last sample, get a new sample
*pfNewSample = TRUE;

}
}
CString szdiag;

if (*pfNewSample) {
if ((DWORD) InterlockedExchange((PLONG) &m_msLastTick, msCurrentTick) == msLastTick) {

PdhCollectQueryData(m_hQuery);
PDH_FMT_COUNTERVALUE pdhfcv;
PDH_STATUS stat = PdhGetFormattedCounterValue(m_hCounter, PDH_FMT_LONG, NULL, &pdhfcv);
szdiag.Format("GetStatus::PdhGetFormattedCounterValue returned %x, CStatus = %x",
stat,pdhfcv.CStatus);
TRACEX(szdiag);
if (pdhfcv.CStatus != 0) return -1; // This indicate erro (CPU doesn't exist for example)
m_nLastSampledCPUUsage = pdhfcv.longValue;
} else {
*pfNewSample = FALSE;
}
}
return(m_nLastSampledCPUUsage);
}


I create an object then call its Init method. For some reason, I don't understand, the PdhOpenQuery call returns success even if the CPU doesn't exist!! So I make an additional call to GetUsage, which returns -1 if there the requested processor doesn't exist.

Usage:
CArray<CCPUUsage*, CCPUUsage *> cpus;
int nCPUCount = 0;
while (TRUE)
    cpus[nCPUCount] = new CCPUUSAge();
    cpus[nCPUCount].Init(50,nCPUCount);
    if (cpus[nCPUCount].GetUsage == -1) break;
    nCPUCount++
}



Hope this helps
Bill
GeneralRe: Task Manager process CPU usage? Pin
Peter Sjöström23-Oct-01 9:10
Peter Sjöström23-Oct-01 9:10 
GeneralRe: Task Manager process CPU usage? Pin
Bill Wilson23-Oct-01 9:44
Bill Wilson23-Oct-01 9:44 
GeneralRe: Task Manager process CPU usage? Pin
Peter Sjöström23-Oct-01 9:48
Peter Sjöström23-Oct-01 9:48 
GeneralSolved! (new language question) Pin
Peter Sjöström23-Oct-01 9:46
Peter Sjöström23-Oct-01 9:46 
GeneralHBITMAP and unsigned char array Pin
mkaltner22-Oct-01 13:13
mkaltner22-Oct-01 13:13 
GeneralRe: HBITMAP and unsigned char array Pin
Shog922-Oct-01 16:51
sitebuilderShog922-Oct-01 16:51 
GeneralQuestion about operator new Pin
22-Oct-01 13:04
suss22-Oct-01 13:04 
GeneralRe: Question about operator new Pin
Bret Faller22-Oct-01 13:41
Bret Faller22-Oct-01 13:41 
QuestionWhich trackbar fired the event? Pin
clintsinger22-Oct-01 12:55
clintsinger22-Oct-01 12:55 
AnswerRe: Which trackbar fired the event? Pin
Christian Graus22-Oct-01 13:07
protectorChristian Graus22-Oct-01 13:07 
GeneralRe: Which trackbar fired the event? Pin
clintsinger22-Oct-01 13:16
clintsinger22-Oct-01 13:16 
GeneralRe: Which trackbar fired the event? Pin
Christian Graus22-Oct-01 13:57
protectorChristian Graus22-Oct-01 13:57 
GeneralRe: Which trackbar fired the event? Pin
Michael Dunn22-Oct-01 15:56
sitebuilderMichael Dunn22-Oct-01 15:56 
GeneralRe: Which trackbar fired the event? Pin
clintsinger22-Oct-01 17:02
clintsinger22-Oct-01 17:02 
GeneralRe: Which trackbar fired the event? Pin
Christian Graus22-Oct-01 18:19
protectorChristian Graus22-Oct-01 18:19 
GeneralRe: Which trackbar fired the event? Pin
clintsinger24-Oct-01 6:21
clintsinger24-Oct-01 6:21 
GeneralRe: Which trackbar fired the event? Pin
Christian Graus24-Oct-01 11:22
protectorChristian Graus24-Oct-01 11:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.