找回密码
 加入
搜索
查看: 4046|回复: 4

[AU3基础] 求一个得到CPU占用率的函数

[复制链接]
发表于 2012-7-21 23:12:38 | 显示全部楼层 |阅读模式
搜索了很久,也没有找到能得到CPU占用率的函数,很多都是WMI的,看到P版说有2NtQuerySystemInformation和PDH的方法,但是都不知道怎样实现。有谁能够提供这2个方法实现的函数不胜感激啊!
 楼主| 发表于 2012-7-22 19:47:49 | 显示全部楼层
这2天一直在看P版的Pdh.au3里面的例子,可是还是没有看懂,希望P版能够提供一个得到CPU使用率和内存使用率的例子。
发表于 2012-7-27 15:29:38 | 显示全部楼层
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
Global $IDLETIME, $KERNELTIME, $USERTIME,$StartIdle, $StartKernel, $StartUser,$EndIdle, $EndKernel, $EndUser

$hWnd = GUICreate("CPU监控", 130, 240, -1, -1, BitOR($WS_SYSMENU, $WS_CAPTION, $WS_POPUP), 0)

$hMem_Progress = GUICtrlCreateProgress(20, 40, 29, 169, BitOR($PBS_SMOOTH, $PBS_VERTICAL))
$hCPU_Progress = GUICtrlCreateProgress(80, 40, 29, 169, BitOR($PBS_SMOOTH, $PBS_VERTICAL))
$hMem_Label = GUICtrlCreateLabel("内存", 22, 215, 37, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$hCPU_Label = GUICtrlCreateLabel("CPU", 80, 215, 35, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$hMem_Show = GUICtrlCreateLabel("", 25, 20, 37, 20)
$hCPU_Show = GUICtrlCreateLabel("", 84, 20, 35, 20)
$IDLETIME = DllStructCreate("dword;dword")
$KERNELTIME = DllStructCreate("dword;dword")
$USERTIME = DllStructCreate("dword;dword")
AdlibRegister("_TimerProc", 1000)
GUISetState(@SW_SHOW)
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd

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

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))
        Local $CPU = $iTotal
        Local $tArr = MemGetStats()
        Local $RAM = $tArr[0]
        If $CPU & "%" <> GUICtrlRead($hCPU_Show) And ($CPU >= 0 And $CPU <= 100) Then
                GUICtrlSetData($hCPU_Progress, $CPU)
                GUICtrlSetData($hCPU_Show, $CPU & "%")
        EndIf
        If $RAM & "%" <> GUICtrlRead($hMem_Show) And ($RAM >= 0 And $RAM <= 100) Then
                GUICtrlSetData($hMem_Progress, $RAM)
                GUICtrlSetData($hMem_Show, $RAM & "%")
        EndIf
EndFunc   ;==>_CPUCalc
 楼主| 发表于 2012-8-6 11:43:05 | 显示全部楼层
回复 3# 鸟人


    多谢了,可是我没有看懂,我太菜了,呵呵。
能不够帮我改改,比如当前我看见的任务管理器里CPU使用时20%,我调用_CPUCalc()函数就能得到返回值20或者20%,非常感谢帮组啊。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-20 12:03 , Processed in 0.080728 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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