Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)