I have an application that must find out the current load on the system on a regular basis. I'm using WMI/WBEM to do this. The problem is that one of the WMI calls takes about 1/2 second to return...

This is a BIG problem.

The call that takes so long is the ExecQuery call that only happens once per calculation. The loop gets the LoadPercentage for each proecssor on the system. (I think). I need the enumerator in anycase.

Here's my code:


long CSysLoad::GetLoadPercentage()
{



// Perform the query
IEnumWbemClassObject *pEnum = 0;

try
{
// ***********************The next line takes 1/2 second
hres = pSvc->ExecQuery( language,query,/*BEM_FLAG_FORWARD_ONLY*/ 0x20,NULL,&pEnum);
}
catch (CException *e)
{
}
if(FAILED(hres))
{

CErrorInfo err;
GET_ERROR_MSG(lpMsg, hres);
err.SetError(hres,CString((LPCSTR) lpMsg ));
throw err;
return 0;
}

long lReturnValue = 0;
// Define the object interface (AND initialize it to NULL)
IWbemClassObject *pObj = 0;
ULONG uReturned = 0; // Used to check the number of objects returned

long lTotalLoad = 0;
int iProcessorCount = 0;

while(true)
{

// Check each processor, average result

hres = pEnum->Next( 0,1,&pObj,&uReturned);

if(uReturned == 0) // If no more to process, uReturned is set to 0 so we're done
break;

// Since we're not done, extract the LoadPercentage from the query result


BSTR strClassProp = SysAllocString(L"LoadPercentage");
VARIANT v;

hres = pObj->Get(strClassProp, 0, &v, 0, 0);
SysFreeString(strClassProp);


// If the property is found, convert our variant to a number.

if(!FAILED(hres))
{
/* bstr_t temp(v);
TCHAR buf[128];

_tcscpy(buf, (LPTSTR)temp);

long lResult = atoi(buf);
*/ if (v.vt = VT_I4)
{
lTotalLoad += v.iVal;
iProcessorCount++;
}

} else {

CErrorInfo err;
GET_ERROR_MSG(lpMsg, hres);
err.SetError(hres,CString((LPCSTR) lpMsg ));
throw err;
return 0;

}

VariantClear(&v);

pObj->Release();

}
// Calculate the answer


return (iProcessorCount > 0) ? lTotalLoad / iProcessorCount : 0;
// If this doesn't work, we will be running WIDE OPEN!!!


}





Thanks for the help,
Bill