找回密码
 加入
搜索
查看: 5324|回复: 11

请问如何检测CPU的使用率?

[复制链接]
发表于 2008-6-22 20:07:11 | 显示全部楼层 |阅读模式
就好像Windows的任务管理器一样,可以显示CPU的总使用率,要怎么做?

我得到过如下代码:
;;_ProcessGetCPU("进程ID 留空时的返回系统空闲百分比","刷新间隔")
Func _ProcessGetCPU($strProcess = "Idle", $iSampleTime = 500, $sComputerName = @ComputerName)
    if $strProcess = "" then $strProcess = "Idle"
    if $iSampleTime = "" AND IsString($iSampleTime) then $iSampleTime = 500
    if $sComputerName = "" then $sComputerName = @ComputerName   
    if not IsDeclared("iP1") AND $iSampleTime = 0 then    ;first time in loop mode
        $bFirstTimeInLoopMode = 1
    else
        $bFirstTimeInLoopMode = 0
    endif   
    if not IsDeclared("iP1") then
        assign("iP1", 0, 2)  ;forced global declaration first time
        assign("iT1", 0, 2)
    endif   
    $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2")
    if @error then return -2   
    if number($strProcess) then
        $strProcess = " WHERE IDProcess = '" & $strProcess & "'"
    else
        $strProcess = " WHERE Name = '" & $strProcess & "'"
    endif
    if $iSampleTime OR $bFirstTimeInLoopMode = 1 then   ;skip if Loop Mode, but not First time       
        $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)       
        For $objItem In $colItems           
            $iT1 = $objItem.TimeStamp_Sys100NS
            $iP1 = $objItem.PercentProcessorTime
        next
        if  $objItem = "" then return -1    ;process not found       
        sleep($iSampleTime)
    endif       
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)
    For $objItem In $colItems                   
        $iP2 = $objItem.PercentProcessorTime
        $iT2 = $objItem.TimeStamp_Sys100NS 
    next
    if  $objItem = "" then return -1    ;process not found       
    $iPP = ($iP2 - $iP1)
    $iTT = ($iT2 - $iT1)   
    if $iTT = 0 Then return 100  ;do not divide by 0
    $iCPU = round( ($iPP/$iTT) * 100, 0)   
    $iP1 = $iP2
    $iT1 = $iT2
    Return $iCPU
EndFunc


但这段代码在碰到多核CPU(双核、三核,四核未测试)时会出现错误,例如显示CPU空闲为179%等状况。由于这段代码我没看很懂,所以还请高手赐教。

[ 本帖最后由 skyfree 于 2008-6-23 09:39 编辑 ]
发表于 2008-6-23 08:22:59 | 显示全部楼层
$wbemServices = ObjGet("winmgmts:\\.\root\cimv2") 
$wbemObjectSet= $wbemServices.ExecQuery("Select * from Win32_Processor")
For $wbemObject In $wbemObjectSet 
$dd=$wbemObject.LoadPercentage
Next
MsgBox(0,"cpu使用率:",$dd)


这个看下?

[ 本帖最后由 renren 于 2008-6-23 08:24 编辑 ]
 楼主| 发表于 2008-6-23 09:39:24 | 显示全部楼层
AdlibEnable("CPURate",1000)

While 1
WEnd

Func CPURate()
        TrayTip("CPU","CPU使用率:"&CurrentCPURate(),"")
EndFunc

Func CurrentCPURate()
        $wbemServices = ObjGet("winmgmts:\\.\root\cimv2") 
        $wbemObjectSet= $wbemServices.ExecQuery("Select * from Win32_Processor")
        For $wbemObject In $wbemObjectSet 
                $dd=$wbemObject.LoadPercentage
        Next
        Return $dd
EndFunc


谢谢版主提示,我做成了上面的一小段程序,间隔一秒读取CPU使用率,方便大家测试啦~~

再次感谢!
 楼主| 发表于 2008-6-23 10:16:02 | 显示全部楼层
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=D:\Personal\Desktop\CPU_RAM.kxf
$FormName="System"
$Form1 = GUICreate($FormName, 306, 70, 193, 125, BitOR($WS_MINIMIZEBOX,$WS_SYSMENU,$WS_POPUP,$WS_GROUP,$WS_CLIPSIBLINGS))
$Label1 = GUICtrlCreateLabel("CPU", 8, 8, 36, 17)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$Label2 = GUICtrlCreateLabel("RAM", 8, 40, 36, 17)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$Progress1 = GUICtrlCreateProgress(56, 8, 193, 17)
$Progress2 = GUICtrlCreateProgress(56, 40, 193, 17)
$Label3 = GUICtrlCreateLabel("", 264, 8, 36, 20)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$Label4 = GUICtrlCreateLabel("", 264, 40, 36, 17)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$pos = WinGetPos($FormName)
WinMove($FormName, "", @DesktopWidth / 2 - $pos[2] / 2, @DesktopHeight / 2 - $pos[3] / 2)
WinSetOnTop($FormName,"",1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

AdlibEnable("CpuRamRate",500)

While 1
        
WEnd

Func CpuRamRate()
        Local $rCPU,$rRAM
        $rCPU=CurrentCPURate()
        $rRAM=CurrentRAMRate()
        GUICtrlSetData($Progress1,$rCPU)
        GUICtrlSetData($Label3,$rCPU&"%")
        GUICtrlSetData($Progress2,$rRAM)
        GUICtrlSetData($Label4,$rRAM&"%")
EndFunc

Func CurrentCPURate()
        $wbemServices = ObjGet("winmgmts:\\.\root\cimv2") 
        $wbemObjectSet= $wbemServices.ExecQuery("Select * from Win32_Processor")
        For $wbemObject In $wbemObjectSet 
                $dd=$wbemObject.LoadPercentage
        Next
        Return $dd
EndFunc

Func CurrentRAMRate()
        Local $Array[7]
        $Array=MemGetStats()
        Return $Array[0]
EndFunc


随手写了个GUI界面,附带内存使用率,贡献给论坛吧~~呵呵
发表于 2008-6-23 10:39:55 | 显示全部楼层
高手的问题级别也高。
发表于 2008-6-23 13:21:41 | 显示全部楼层
赞楼主做的漂亮。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2009-6-6 19:58:31 | 显示全部楼层
PF使用率好像不对啊~~~
发表于 2009-6-6 20:11:36 | 显示全部楼层
好贴留名,不过内存使用好象有点怪
发表于 2009-6-7 09:30:18 | 显示全部楼层
呵呵,又是任务管理器的功能:)
发表于 2009-6-10 16:56:46 | 显示全部楼层
OK
发表于 2009-10-15 14:30:43 | 显示全部楼层
对于多核cpu,以上使用率是不准确的,读取的是最后一个处理器的使用率,不是综合使用率
发表于 2015-7-17 11:18:19 | 显示全部楼层
回复 4# skyfree
S大您好:能加个获取内存的大小吗?如:内存使用率:0.3G / 2GB
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-28 18:24 , Processed in 0.092358 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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