2

我一直在尝试获取我正在创建的简单桌面应用程序的 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>
4

1 回答 1

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>
于 2012-04-14T14:32:46.270 回答