Jump to content
Sign in to follow this  
TarwadaC4

From tip to Progress

Recommended Posts

TarwadaC4

hello guys

checkout this Code

$array = ProcessList("explorer.exe")
$iPID = $array[1][1]
$sProcess = $iPID          ;PID number for test process name
;~ $sProcess = "Idle"      ;Other test process names
;~ $sProcess = "_Total"  ;funny one
;~ $sProcess = "ntvdm"    ;DOS process
;~ $sProcess = "AutoIt3"    ;do not assign .exe to process name

While 1
    $iProcessCPU = _ProcessGetCPU($sProcess, 300 )
    $sTip = "Process " & $sProcess & " CPU: " & $iProcessCPU & "%"
    traytip("", $sTip   ,1)
;~  sleep(1000)  ;set your own sleep time for LOOP mode
WEnd

Func _ProcessGetCPU($strProcess = "Idle", $iSampleTime = 10, $sComputerName = @ComputerName)
;~    All Parameters are optional:
;~        - Idle process will be measured if first parameter is not set
;~        - 500 ms is default sample time
;~        - This computer will be measured by default
;~    Process could be string ("Name") or PID number (1234)
;~    When more processes are runing with identical name, then the last opened is measured (use PID for other)
;~    For NORMAL MODE(one time measuring): set Sample value to more than 0 ms
;~                ( average CPU usage will be measured during sleep time within function)
;~    For LOOP MODE (continuous measuring): set Sample value to 0 ms
;~                ( average CPU usage will be measured between two function calls )
;~    Total CPU usage is: ( 100 - _ProcessGetCPU())
;~    Success: Returns process CPU usage in percent
;~            (Sample times below 100ms may return inaccurate results)
;~            (First result in Loop Mode may be inaccurate,
;~             because first call in Loop Mode is only used to trigger counters)
;~    Failure: Returns -1  ( wrong process name or PID )
;~           : Returns -2  ( WMI service not found or Computer not found)
    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 ;==>_ProcessGetCPU() by novaTek ...ver 0.11

ok as you see this will show you your Explorer.exe in tip, im wondering about how to make this show you the CPU Usage in The Progress bar ;)

Lets say we will put the CPU usage in this progress bar :)

#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 129, 192, 124)
$Progress1 = GUICtrlCreateProgress(32, 16, 545, 89)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
WEnd

i hope someone know how to .. :D

Share this post


Link to post
Share on other sites
Zedna

This is quick solution.

It can be improved to be called according to timers.

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

Global $iProcessCPU_prev = ''

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 129, 192, 124)
$Progress1 = GUICtrlCreateProgress(32, 16, 545, 89)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;~ $array = ProcessList("explorer.exe")
;~ $iPID = $array[1][1]
;~ $sProcess = $iPID          ;PID number for test process name
$sProcess = "Idle"      ;Other test process names
;~ $sProcess = "_Total"  ;funny one
;~ $sProcess = "ntvdm"    ;DOS process
;~ $sProcess = "AutoIt3"    ;do not assign .exe to process name

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
 Case $GUI_EVENT_CLOSE
  Exit
EndSwitch

    $iProcessCPU = _ProcessGetCPU($sProcess, 300 )
If $iProcessCPU <> $iProcessCPU_prev Then
GUICtrlSetData($Progress1, $iProcessCPU)
$iProcessCPU_prev = $iProcessCPU
;~     $sTip = "Process " & $sProcess & " CPU: " & $iProcessCPU & "%"
;~     traytip("", $sTip   ,1)
EndIf
WEnd

Func _ProcessGetCPU($strProcess = "Idle", $iSampleTime = 10, $sComputerName = @ComputerName)
;~    All Parameters are optional:
;~        - Idle process will be measured if first parameter is not set
;~        - 500 ms is default sample time
;~        - This computer will be measured by default
;~    Process could be string ("Name") or PID number (1234)
;~    When more processes are runing with identical name, then the last opened is measured (use PID for other)
;~    For NORMAL MODE(one time measuring): set Sample value to more than 0 ms
;~                ( average CPU usage will be measured during sleep time within function)
;~    For LOOP MODE (continuous measuring): set Sample value to 0 ms
;~                ( average CPU usage will be measured between two function calls )
;~    Total CPU usage is: ( 100 - _ProcessGetCPU())
;~    Success: Returns process CPU usage in percent
;~            (Sample times below 100ms may return inaccurate results)
;~            (First result in Loop Mode may be inaccurate,
;~             because first call in Loop Mode is only used to trigger counters)
;~    Failure: Returns -1  ( wrong process name or PID )
;~           : Returns -2  ( WMI service not found or Computer not found)
    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 & "rootCIMV2")
    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 ;==>_ProcessGetCPU() by novaTek ...ver 0.11
  • Like 1

Share this post


Link to post
Share on other sites
TarwadaC4

man

you are awesome ;)

:)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • argumentum
      By argumentum
      The back story:
      I've got a Dell XPS w/ i7-8700K. The fastest, by single core, I could get, by well known PC maker.
      The problem is that the fan can get so loud, like, REALLY LOUD, I can not use the CPU at its max. clock speed.
      I could leave it at 90% all the time and not use this but I want to have the full 4.x Ghz and no parked cores, at all times, if I can.
      But as room temperature and CPU load changes, a set throttle, may still make fan noise.
      The solution:
      To avoid the fan from going "airplane turbine mode", the utility gets the temp. from "Core Temp" ( you can google it )
      It has a"plug-in" called "Core Temp Remote Server". The utility gets the values via TCP.
      When it "feels" it's gonna get hot, drops the CPU throttle to a selected value, lets say 99% ( where is quieter ) and back up to 100% when it "feels" is ok to go back.
      Now temperature can creep up to higher than expected if load is sustained or room temperature changes. So there is an "anti creep up" feature, to temporarily set the throttle even lower, 5% at a time, until the known quiet temperature is achieved.
      If don't know how to find the temperature you should use, check out these videos. They will tell you how.
      https://www.youtube.com/watch?v=p3B5WCJZTuw&ab_channel=SergeantPope-KomadaComputerRepair
      https://www.youtube.com/watch?v=VuP6I0mOb1s&ab_channel=Techquickie
      https://www.google.com/search?q=find+max+cpu+temperature
      The end result:
      Any thermal problem, is a hardware problem. No way around that, other than attending to the CPU cooling and case ventilation. Software can not fix that.
      But without this utility, the PC would slow down the CPU anyway, to keep it from melting.
      This software preemptively slow down the CPU, keeping the CPU related fan speeds from going to maximum RPM. Hence having a slower, but a quieter box.
    • red0fireus
      By red0fireus
      Hello, I'm very stumped with finding the decimal place for the CPU %.
      I found some example code online
      #include <Array.au3> $aProcess_Info = GetCPU("RzSynapse") _ArrayDisplay($aProcess_Info) Func GetCPU($sProcess = "") Local $aAllProcess_Info[1000][3] = [[0]], $aOneProcess_Info[3] $oWinMgmts = ObjGet("winmgmts:\\.\root\cimv2") $aCPU_Usage = $oWinMgmts.ExecQuery("SELECT Name, IDProcess, PercentProcessorTime FROM Win32_PerfFormattedData_PerfProc_Process") For $vCPU_Info In $aCPU_Usage If $sProcess Then If $vCPU_Info.Name = $sProcess Then $aOneProcess_Info[0] = $sProcess $aOneProcess_Info[1] = $vCPU_Info.IDProcess $aOneProcess_Info[2] = $vCPU_Info.PercentProcessorTime & " %" Return $aOneProcess_Info EndIf Else $aAllProcess_Info[0][0] += 1 $aAllProcess_Info[$aAllProcess_Info[0][0]][0] = $vCPU_Info.Name $aAllProcess_Info[$aAllProcess_Info[0][0]][1] = $vCPU_Info.IDProcess $aAllProcess_Info[$aAllProcess_Info[0][0]][2] = $vCPU_Info.PercentProcessorTime & " %.1f" EndIf Next ReDim $aAllProcess_Info[$aAllProcess_Info[0][0] +1][3] Return $aAllProcess_Info EndFunc ;==>GetCPU Currently the code shows the CPU % as 0 % and I want to show 0.0 % I found this online for strings "%.1f" but i'm not sure how to incorporate it.
    • francoiste
      By francoiste
      in AutoIt we have:
      _WinAPI_IsProcessorFeaturePresent($iFeature) this internally just calls what is provided by microsoft: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724482(v=vs.85).aspx
      unfortunately this is completely outdated as we can only check for features like MMX and up to a maximum of SSE2 or SSE3 - which have been introduced back in 2001 / 2004.
      but nowadays there is stuff like AVX, AVX2, AVX-512 ...
      for example see: https://en.wikipedia.org/wiki/Template:Multimedia_extensions
      well, for CPP we have the FeatureDetector (by Mysticial): https://github.com/Mysticial/FeatureDetector
      it calls __cpuid / __cpuidex from <intrin.h>.
      for additional details see: https://msdn.microsoft.com/en-us/library/hskdteyh.aspx
       
      has anyone tried to determine such SIMD features and related CPU capabilites with AutoIt?
    • VIP
      By VIP
      The simple widget shows the percentage of cpu, ram used and the CPU temperature (WMI).
      - Every 30s check, sync and update time for computer.
      - Drag adn Drop any files on GUI for reset Attribute (-RAHS) and set Full Access to Everyone !
      - Righ Click on x and click Un/Install app to boot with Windows!
      CPU_Widget_TimeSync.exe CRC32: 1956339E MD5: BFC2B596BD1EDA35D6B02B97F8FB57AE SHA-1: 796A663739EA5AC9890D9FA324A98E987F35E7C8
      Function code from:
      CPU: 
      RAM:  MemGetStats ( )
      Memory:   _WinAPI_EmptyWorkingSet ()
       
    • nacerbaaziz
      By nacerbaaziz
      hello
      please i need to link a progress bar with a time can you help me?
      e.g
      i want to set a progress bar for 10 sec
      am waiting for your answers
      thank you.
×