#include <Windows.h> DWORD Caculation_CPU(LPVOID lpVoid) { MEMORYSTATUS MemoryInfo; DWORD PercentIdle = 0; while(1) { DWORD dwStartTick = GetTickCount(); DWORD dwIdleSt = GetIdleTime(); Sleep(1000); DWORD dwStopTick = GetTickCount(); DWORD dwIdleEd = GetIdleTime(); PercentIdle = 100 - ((100*(dwIdleEd - dwIdleSt)) / (dwStopTick - dwStartTick)); GlobalMemoryStatus(&MemoryInfo); RETAILMSG(1,(_T("dwTotalPhys = %d, dwAvailPhys = %d,dwAvailVirtual = %d, System CPU: Usage is %d%% !!"),MemoryInfo.dwTotalPhys, MemoryInfo.dwAvailPhys, MemoryInfo.dwAvailVirtual, PercentIdle)); } return 0; } //创建一个线程来计算CPU和内存占用率 DWORD dwThreadID = 0;
CreateThread(NULL,0,Caculation_CPU,0,NULL,&dwThreadID);