标签:
直接上代码:
1 /* 2 在 OEMIdle() 函数中 OEM 根据 (int)(dwRescheduleTime – CurMSec) 3 计算值判断是否让 CPU 进入 idle 状态,因此我们可以利用此函数来计算 CPU 的 loading. 4 根据 MSDN ,可以写个应用程序,每 100ms 去计算一次 idle 的时间, 5 10 次评价后,在UI上更新,以下是一个被 Timer 循环调用的函数。建议将此函数写成的一个线程。 6 */ 7 8 void CSystemInfoDlg::GetCpuStatus(CString &csInfo) 9 { 10 #define IDLE_COUNT 10 11 DWORD dwStartTick = 0; 12 DWORD dwIdleStart = 0; 13 DWORD dwStopTick = 0; 14 DWORD dwIdleEnd = 0; 15 int iPresentIdle = 0; 16 int iCpuUage[IDLE_COUNT] = {0}; 17 int iCount = 0; 18 int iAverage = 0; 19 static int iPreAverage = 0; 20 DWORD dwSum = 0; 21 int i = 0; 22 23 while(iCount < IDLE_COUNT) 24 { 25 dwStartTick = GetTickCount(); 26 dwIdleStart = GetIdleTime(); 27 Sleep(100); 28 29 dwStopTick = GetTickCount(); 30 dwIdleEnd = GetIdleTime(); 31 32 iPresentIdle = (100 * (dwIdleEnd - dwIdleStart)) / (dwStopTick - dwStartTick); 33 iCpuUage[iCount] = iPresentIdle; 34 iCount++; 35 //10 times count 36 if(IDLE_COUNT == iCount) 37 { 38 for(i = 0;i < IDLE_COUNT;i++) 39 { 40 dwSum += iCpuUage[i]; 41 } 42 iAverage = dwSum / IDLE_COUNT; 43 44 if(iPreAverage != iAverage) 45 { 46 iPreAverage = iAverage; 47 } 48 RETAILMSG(1,(TEXT("CPU used is:%d%%\r\n"),100 - iAverage)); 49 csInfo.Format(L"CPU used is:%d%%.",100 - iAverage); 50 51 break; 52 } 53 } 54 }
标签:
原文地址:http://www.cnblogs.com/91program/p/5206474.html