我一直在尝试获取我正在创建的简单桌面应用程序的 CPU 使用百分比。
我已经将几个 VBS 脚本一起放入了一个似乎可以工作的 HTA,但它不是很准确,或者至少我不这么认为,如果我与任务管理器并排运行它 -
我正在使用 Win32_PerfRawData_PerfOS_Processor 中的“Total”
你能看一下,看看有什么我可以做的更好吗?
提前致谢
<html>
<head>
<script type='text/javascript'>
window.resizeTo(240,150);
</SCRIPT>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="description" content="Created by David Brennan, adapted from a couple of scripts - 1. To Get CPU Percentage - http://msdn.microsoft.com/en-us/library/windows/desktop/aa392397(v=vs.85).aspx and 2. To run the script over and over again - http://www.windowsitpro.com/article/vbscript/utility-monitors-crucial-processes">
<title>CPU + RAM usage</title>
<HTA:APPLICATION
APPLICATIONNAME="CPU Usage Percentage"
ID="cpuUsagePercentage"
VERSION="0.1"
BORDER="dialog"
SCROLL="no"
CONTEXTMENU="no"
SINGLEINSTANCE="no"
WINDOWSTATE="normal"/>
<style type="text/css">
h3{color:purple;}
</style>
</head>
<body>
<div id='DataArea'>
waiting for info
</div>
<script language="VBScript">
'************************************************************************************************
Sub Window_OnLoad
Dim refrtime
refrTime = 1000 'Refresh the screen every 1000 millisecs (one seconds)
GetPercentage(refrtime)
TimeoutID = window.setInterval("GetPercentage(" & refrtime &")",refrTime)
End sub
'************************************************************************************************
Sub GetPercentage(refrtime)
Set objService = GetObject( _
"Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
Set objInstance1 = objService.Get( _
"Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N1 = objInstance1.PercentProcessorTime
D1 = objInstance1.TimeStamp_Sys100NS
Set perf_instance2 = objService.Get( _
"Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N2 = perf_instance2.PercentProcessorTime
D2 = perf_instance2.TimeStamp_Sys100NS
' Look up the CounterType qualifier for the PercentProcessorTime
' and obtain the formula to calculate the meaningful data.
' CounterType - PERF_100NSEC_TIMER_INV
' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100
CPUpercentage = Round(PercentProcessorTime,2)
DataArea.InnerHTML = "<h3>CPU Usage = " & CPUpercentage & "%"
End sub
'************************************************************************************************
</script>
</body>
</html>