Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CPU utilization for a particular process
#1
Is there a way to get the CPU utilization for a particular process?
#2
Function GetProcessCpuUsage

Code:
Copy      Help
[color=green];/[/color]
[color=blue]function[/color]# hwnd

[color=green];Returns CPU usage (0 - 100 %) of program to which belongs window hwnd.
;Measure time is 100 ms. Precision is 10% or worse. Doesn't work on Win 98/Me.[/color]

[color=green];EXAMPLE
;int hwnd=win("Windows Media Player")
;rep[/color]
,[color=green];1[/color]
,[color=green];out GetProcessCpuUsage(hwnd)[/color]


[color=blue]#if[/color] _winnt
[color=blue]#compile[/color] "toolhelp"
[color=blue]dll [/color]kernel32 #GetProcessTimes hProcess FILETIME*lpCreationTime FILETIME*lpExitTime FILETIME*lpKernelTime FILETIME*lpUserTime

[color=blue]int [/color]pid hp
[color=blue]long [/color]tce tkernel tuser t1 t2

GetWindowThreadProcessId(hwnd &pid)
hp=OpenProcess(PROCESS_QUERY_INFORMATION 0 pid); [color=blue]if[/color](!hp) [color=blue]end [/color]ES_FAILED

[color=blue]opt [/color]waitmsg -1
GetProcessTimes hp +&tce +&tce +&tkernel +&tuser
t1=tkernel+tuser
0.1
GetProcessTimes hp +&tce +&tce +&tkernel +&tuser
t2=tkernel+tuser

CloseHandle hp

t2-t1
t2/10000
[color=blue]if[/color](t2>100) t2=100
[color=blue]ret [/color]t2
#3
Awesome!!
Thanks.
#4
what am i doing wrong here?
i keep getting "Error (RT) in CPU_Notice: failed"
Code:
Copy      Help
int hwnd=win("Crystal Reports")
int a b
rep
    a=(GetProcessCpuUsage(hwnd))
    5
    b=(GetProcessCpuUsage(hwnd))
    if a+b=0
        mes("Crystal Reports is done." "CPU Notice" "i")
        end
thanks
#5
Probably the window is closed. Use IsWindow to test whether hwnd is still valid. Or use err.

This code shows error description:

a=GetProcessCpuUsage(hwnd)
err out _s.dllerror
#6
hmmmm..... it was running but maybe an issue with the virtual desktop thing (i ditched litestep and went with VirtuaWin V2.10 from Sourceforge (so you know it's completly bug proof)).
:wink:


thanks
#7
How would I get the total system CPU usage ?
#8
GetCPU


Forum Jump:


Users browsing this thread: 1 Guest(s)