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

スクリプトを作り直して、2つの測定の間に一時停止しました。これで、タスクマネージャーの結果と同等になりました。

<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 に答える