CPU Auslastung

    • Offizieller Beitrag
    Spoiler anzeigen
    [autoit]

    #include <ProgressConstants.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    Local $CenterWidth = (@DesktopWidth - 200) / 2
    Local $CenterHeight = (@DesktopHeight - 300) / 2

    [/autoit] [autoit][/autoit] [autoit]

    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $strComputer = "localhost"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

    [/autoit] [autoit][/autoit] [autoit]

    ; ============================================================================
    ; GUI Definition
    ; ============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    $gui = GUICreate("CPU Meter", 200, 300, $CenterWidth, $CenterHeight, -1, $WS_EX_TOPMOST)
    GUISetOnEvent(-3, "_Exit_Event", $gui)
    GUISetIcon(@SystemDir & "\shell32.dll", 12)

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlCreateLabel("CPU1", 5, 3, 40, 20, $SS_CENTER)
    $progressbar_core1 = GUICtrlCreateProgress(10, 20, 30, 250, $PBS_VERTICAL)
    $label_core1 = GUICtrlCreateLabel("", 10, 275, 30, 20, $SS_CENTER)

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlCreateLabel("CPU2", 55, 3, 40, 20, $SS_CENTER)
    $progressbar_core2 = GUICtrlCreateProgress(60, 20, 30, 250, $PBS_VERTICAL)
    $label_core2 = GUICtrlCreateLabel("", 60, 275, 30, 20, $SS_CENTER)

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlCreateLabel("CPU3", 105, 3, 40, 20, $SS_CENTER)
    $progressbar_core3 = GUICtrlCreateProgress(110, 20, 30, 250, $PBS_VERTICAL)
    $label_core3 = GUICtrlCreateLabel("", 110, 275, 30, 20, $SS_CENTER)

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlCreateLabel("CPU4", 155, 3, 40, 20, $SS_CENTER)
    $progressbar_core4 = GUICtrlCreateProgress(160, 20, 30, 250, $PBS_VERTICAL)
    $label_core4 = GUICtrlCreateLabel("", 160, 275, 30, 20, $SS_CENTER)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    ; ============================================================================
    ; Main Loop
    ; ============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $colItems = $objWMIService.ExecQuery ("SELECT LoadPercentage FROM Win32_Processor", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
    $i = 0
    For $objItem In $colItems
    If $i = 0 Then
    GUICtrlSetData($progressbar_core1, $objItem.LoadPercentage)
    GUICtrlSetData($label_core1, $objItem.LoadPercentage & "%")
    ElseIf $i = 1 Then
    GUICtrlSetData($progressbar_core2, $objItem.LoadPercentage)
    GUICtrlSetData($label_core2, $objItem.LoadPercentage & "%")
    ElseIf $i = 2 Then
    GUICtrlSetData($progressbar_core3, $objItem.LoadPercentage)
    GUICtrlSetData($label_core3, $objItem.LoadPercentage & "%")
    ElseIf $i = 3 Then
    GUICtrlSetData($progressbar_core4, $objItem.LoadPercentage)
    GUICtrlSetData($label_core4, $objItem.LoadPercentage & "%")
    EndIf
    $i += 1
    Next
    Else
    MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_Processor")
    EndIf
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    ; ============================================================================
    ; Functions
    ; ============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit_Event()
    Exit
    EndFunc ;==>_Exit_Event

    [/autoit]
  • Hier auch noch eine :

    Spoiler anzeigen
    [autoit]

    $objRefresher = ObjCreate("WbemScripting.SWbemRefresher")

    [/autoit] [autoit][/autoit] [autoit]

    $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")

    [/autoit] [autoit][/autoit] [autoit]

    $objRefreshableItem = $objRefresher.AddEnum($objWMIService , "Win32_PerfFormattedData_PerfOS_Processor")

    [/autoit] [autoit][/autoit] [autoit]

    $objRefresher.Refresh

    [/autoit] [autoit][/autoit] [autoit]

    If IsObj($objRefreshableItem) Then
    $i = 0
    For $objItem In $objRefreshableItem.ObjectSet
    $i += 1
    Next
    EndIf
    Global $aCPU[$i]

    [/autoit] [autoit][/autoit] [autoit]

    While 1

    [/autoit] [autoit][/autoit] [autoit]

    $objRefresher.Refresh
    If IsObj($objRefreshableItem) Then
    $i = 0
    For $objItem In $objRefreshableItem.ObjectSet
    $aCPU[$i] = $objItem.PercentProcessorTime
    $i += 1
    Next
    EndIf
    ConsoleWrite("============" & @CRLF)
    For $i = 0 To UBound($aCPU)-2
    ConsoleWrite("Core "&$i+1&": "&$aCPU[$i] & @CRLF)
    Next
    ConsoleWrite("Gesamt: "&$aCPU[UBound($aCPU)-1] & @CRLF)
    Sleep(500)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func OnAutoItExit()
    $objRefresher.DeleteAll
    EndFunc

    [/autoit]
  • Wie umständlich ihr denkt o.o

    Spoiler anzeigen
    [autoit]


    ;==============================================
    ; Sen - CpuAuslastung
    ; Return: Cpu Auslastung (%)
    ;==============================================
    Func CpuAuslastung()
    Global $wbemFlagReturnImmediately = 0x10
    Global $wbemFlagForwardOnly = 0x20
    Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
    Global $strComputer = @ComputerName

    [/autoit] [autoit][/autoit] [autoit]

    Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlags)

    [/autoit] [autoit][/autoit] [autoit]

    $n = 0
    $c = 0
    $sMsg = ""
    For $objItem In $colItems
    $sMsg &= "cpu%" & $objItem.LoadPercentage
    $n += 1
    Next

    [/autoit] [autoit][/autoit] [autoit]

    $CpuAuslastung = StringSplit($sMsg, "cpu%", 1)

    [/autoit] [autoit][/autoit] [autoit]

    For $n = "0" To $CpuAuslastung[0]
    $c += $CpuAuslastung[$n]
    Next

    [/autoit] [autoit][/autoit] [autoit]

    Return (Round($c / $CpuAuslastung[0], 0) & "%")
    EndFunc ;==>CpuAuslastung

    [/autoit]

  • Warum umständlich?

    Meins zeigt noch den Speicherverbrauch und die CPU Zeit an, ohne WMI! ;)

    WMI wird nur dazu benutzt die logischen CPUs zu bestimmen!

    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯