请问如何检测CPU的使用率?
就好像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 编辑 ] $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 编辑 ] 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使用率,方便大家测试啦~~
再次感谢! #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, @DesktopHeight / 2 - $pos / 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
$Array=MemGetStats()
Return $Array
EndFunc
随手写了个GUI界面,附带内存使用率,贡献给论坛吧~~呵呵 高手的问题级别也高。 赞楼主做的漂亮。 PF使用率好像不对啊~~~ 好贴留名,不过内存使用好象有点怪 呵呵,又是任务管理器的功能:) :face (22):OK 对于多核cpu,以上使用率是不准确的,读取的是最后一个处理器的使用率,不是综合使用率 回复 4# skyfree
S大您好:能加个获取内存的大小吗?如:内存使用率:0.3G / 2GB
页:
[1]