VBScript監測指定進程的CPU占用率

标簽: , , , , ,

VBScript監測指定進程的CPU占用率,一個程序可能有多個進程實例,這裡隻處理單進程的情況。要處理多個進程也很簡單,有需要者請自行修改。

ProcessName = "ttplayer.exe"

Set objSWbemService = GetObject("winmgmts:\\.\root\cimv2")

set colItems = objSWbemService.ExecQuery("select * from Win32_Processor")
for each objItem in colItems
    CpuNum = CpuNum + objItem.NumberOfLogicalProcessors
next

set colItems = objSWbemService.ExecQuery("select * from Win32_Process " _
    & " where Name = '" & ProcessName & "'")
for each objItem in colItems
    ProcessId = objItem.ProcessId
    exit for
next

set colItems = objSWbemService.ExecQuery( _
    "select * from Win32_PerfFormattedData_PerfProc_Process " _
    & " where IDProcess = " & ProcessId)
for each objItem in colItems
    'WScript.Echo objItem.Path_.RelPath
    set SWbemObject = objItem
next

SWbemObject.Refresh_
do
    SWbemObject.Refresh_
    WScript.StdOut.write vbCr & Time() & vbTab _
        & (SWbemObject.PercentProcessorTime / CpuNum) & "%" & vbTab
    WScript.Sleep 1000
loop

原文鍊接:http://bbs.bathome.net/viewthread.php?tid=18807

随機文章:

  1. 用VBS修改(設置)系統時間和日期
  2. 用VBS獲取圖片分辨率
  3. VB6拾遺:調用C函數(__cdecl調用約定)
  4. WinImage 8.50注冊碼
  5. OpenWrt安裝tcpdump抓包

留下回複