HTA使用WMI监控CPU使用百分比 - 不准确

时间:2012-04-13 12:04:18

标签: vbscript wmi hta

我一直在尝试获取我正在创建的简单桌面应用的CPU使用率。

我已经把几个VBS脚本放到一个似乎有用的HTA中,但它不是很准确,或者至少我不这么认为,如果我和任务管理器一起运行它 - / p>

我正在使用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>

1 个答案:

答案 0 :(得分:1)

我重写了你的脚本,在两次测量之间暂停,现在它与我的任务管理员的结果相同。

<html>
<head>
<script language="vbscript">
  set objservice = getobject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
  const refrtime=1000
</script>
<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
    call getpercentage1()
  end sub
  '--- ---'
  sub getpercentage1()
    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
    timeoutid = window.settimeout("call getpercentage2(" & n1 &","& d1 & ")", refrtime/2)
  end sub
  '--- ---'
  sub getpercentage2(n1, d1)
    set objservice = getobject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
    set perf_instance2 = objservice.get("win32_perfrawdata_perfos_processor.name='_total'")
    n2 = perf_instance2.percentprocessortime
    d2 = perf_instance2.timestamp_sys100ns
    percentprocessortime = (1 - ((n2 - n1)/(d2-d1)))*100
    cpupercentage = round(percentprocessortime,2)
    dataarea.innerhtml = "<h3>cpu usage = " & cpupercentage & "%"
    timeoutid = window.settimeout("getpercentage1()", refrtime)
  end sub
  '--- ---'
  </script>
</body>
</html>
相关问题