找回密码
 加入
搜索
查看: 3755|回复: 6

如何获得一个进程的cpu使用率或内存使用率

[复制链接]
发表于 2008-8-30 19:43:13 | 显示全部楼层 |阅读模式
如何获得一个进程的cpu使用率或内存使用率
autoit能否实现?

[ 本帖最后由 ynygu 于 2008-8-31 07:07 编辑 ]
发表于 2008-8-30 22:14:26 | 显示全部楼层
内存使用率可以直接processgetstate解决
CPU占用要用wmi的process类
发表于 2008-8-31 00:45:07 | 显示全部楼层
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>

Global $IDLETIME, $KERNELTIME, $USERTIME
Global $StartIdle, $StartKernel, $StartUser
Global $EndIdle, $EndKernel, $EndUser
Global $Timer

$IDLETIME   = DllStructCreate("dword;dword")
$KERNELTIME = DllStructCreate("dword;dword")
$USERTIME   = DllStructCreate("dword;dword")

$hGUI = GUICreate("CPUmon", 200, 150, -1, -1, -1, $WS_EX_TOPMOST)
GUISetIcon("shell32.dll", 13)

GUICtrlCreateLabel("Total CPU Usage:", 25, 20, 105, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, "MS Sans Serif")

$ValueLabel = GUICtrlCreateLabel("", 130, 20, 40, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, "MS Sans Serif")

$progress = GUICtrlCreateProgress(170, 20, 20, 120, BitOR($PBS_VERTICAL, $PBS_SMOOTH))
DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle($progress), "wstr", "", "wstr", "")

GUISetState()

_TimerProc()

AdlibEnable("_TimerProc", 1000)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func _TimerProc()
    _GetSysTime($EndIdle, $EndKernel, $EndUser)
    _CPUCalc()
    _GetSysTime($StartIdle, $StartKernel, $StartUser)
EndFunc

Func _GetSysTime(ByRef $sIdle, ByRef $sKernel, ByRef $sUser)
    DllCall("kernel32.dll", "int", "GetSystemTimes", "ptr", DllStructGetPtr($IDLETIME), _
            "ptr", DllStructGetPtr($KERNELTIME), _
            "ptr", DllStructGetPtr($USERTIME))

    $sIdle = DllStructGetData($IDLETIME, 1)
    $sKernel = DllStructGetData($KERNELTIME, 1)
    $sUser = DllStructGetData($USERTIME, 1)
EndFunc   ;==>_GetSysTime

Func _CPUCalc()
    Local $iSystemTime, $iTotal, $iCalcIdle, $iCalcKernel, $iCalcUser
    
    $iCalcIdle   = ($EndIdle - $StartIdle)
    $iCalcKernel = ($EndKernel - $StartKernel)
    $iCalcUser   = ($EndUser - $StartUser)
    
    $iSystemTime = ($iCalcKernel + $iCalcUser)
    $iTotal = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime))
    
    If GUICtrlRead($ValueLabel) <> $iTotal & "%" Then
        ControlSetText($hGUI, "", $ValueLabel, $iTotal & "%")
        GUICtrlSetData($progress, $iTotal)
    EndIf
EndFunc   ;==>_CPUCalc
发表于 2008-8-31 00:48:07 | 显示全部楼层
发表于 2009-9-17 11:12:51 | 显示全部楼层
利用wmi怎么解决的?望指点一二
发表于 2010-9-10 15:04:02 | 显示全部楼层
可以啊。。。
发表于 2011-3-27 21:19:00 | 显示全部楼层
不错 不错 好好学习
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-10-2 10:38 , Processed in 0.087350 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表