11-23-2004, 08:51 PM
Function GetProcessCpuUsage
[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