|
视窗取CPU使用率
<火山程序 类型 = "通常" 版本 = 1 />
方法 取CPU使用率 <公开 静态 类型 = 整数 注释 = "获取cpu使用率" 折叠 @禁止流程检查 = 真>
{
@ FILETIME idleTime, kernelTime, userTime;
@ ULARGE_INTEGER prevIdleTime, prevKernelTime, prevUserTime;
@ prevIdleTime.QuadPart = 0;
@ prevKernelTime.QuadPart = 0;
@ prevUserTime.QuadPart = 0;
@ while (true) {
@ GetSystemTimes(&idleTime, &kernelTime, &userTime);
@ ULARGE_INTEGER currentIdleTime, currentKernelTime, currentUserTime;
@ currentIdleTime.LowPart = idleTime.dwLowDateTime;
@ currentIdleTime.HighPart = idleTime.dwHighDateTime;
@ currentKernelTime.LowPart = kernelTime.dwLowDateTime;
@ currentKernelTime.HighPart = kernelTime.dwHighDateTime;
@ currentUserTime.LowPart = userTime.dwLowDateTime;
@ currentUserTime.HighPart = userTime.dwHighDateTime;
@ if (prevIdleTime.QuadPart != 0) {
@ ULONGLONG idleTicks = currentIdleTime.QuadPart - prevIdleTime.QuadPart;
@ ULONGLONG totalTicks = (currentKernelTime.QuadPart - prevKernelTime.QuadPart) + (currentUserTime.QuadPart - prevUserTime.QuadPart);
@ double cpuUsage = (1.0 - ((double)idleTicks) / totalTicks) * 100.0;
@ return cpuUsage;
@ }
@ prevIdleTime = currentIdleTime;
@ prevKernelTime = currentKernelTime;
@ prevUserTime = currentUserTime;
@ Sleep(1000);
@ }
@ return 0;
}
|
评分
-
查看全部评分
|