Process loading and script loading - who wins?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sethriel

Process loading and script loading - who wins?

24 Sep 2018, 09:04

Hi,

Have a strange issue, tried looping the whole thing, rearranging etc, but no luck. I have this script:

Code: Select all

[code]#Persistent
Loop {

CoordMode, ToolTip, Screen

Process, Exist, skype.exe
PID := errorLevel
; IfEqual, PID, 0, ExitApp
SetTimer, ShowToolTip 
Return

ShowToolTip:
PrLoad := Round( GetProcessTimes(PID),2)
ToolTip %PrLoad%`%, 100,100
If (PrLoad > 9)
	{
	Process, Close, skype.exe
	}
Return


GetProcessTimes( PID )    { 
   Static oldKrnlTime, oldUserTime 
   Static newKrnlTime, newUserTime 

   oldKrnlTime := newKrnlTime 
   oldUserTime := newUserTime 

   hProc := DllCall("OpenProcess", "Uint", 0x400, "int", 0, "Uint", pid) 
   DllCall("GetProcessTimes", "Uint", hProc, "int64P", CreationTime, "int64P"
           , ExitTime, "int64P", newKrnlTime, "int64P", newUserTime) 

   DllCall("CloseHandle", "Uint", hProc) 
Return (newKrnlTime-oldKrnlTime + newUserTime-oldUserTime)/10000000 * 100   
	}
}
[/code]

I borrowed parts of this code from around the net and modded a bit since my Skype (old version on Win10) starts to eat processor for some reason when I'm connected to VPN. Everything in this works when script is loaded after Skype is loaded, but not the other way around - if Skype is loaded after script, it just won't work and the load isn't shown on the ToolTip either. I'm sure there's a perfectly reasonable explanation for this but I just can't see it, so I'd really appreciate it if someone stuck my nose in it. Thanks!

Oh there's another small issue - the ToolTip shows twice the load that Process Explorer shows. I can cheat with the output but I'd also like to understand why it's like that, so if there's any guru outthere with couple of minutes of time, I'd appreciate that too, thanks!

All the best,
A.
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Process loading and script loading - who wins?

24 Sep 2018, 10:14

You have a few things going on here. The first is that your function "GetProcessTime" needs to be outside of your loop as every time the function is called it acts like its in the loop. The second is that you have several "Return" statements within the loop which will cause the loop to exit so you never get past the settimer part of the loop.
sethriel

Re: Process loading and script loading - who wins?

24 Sep 2018, 14:07

Thanks for the effort. However, I had already tried all kinds of different versions of the script, including this one:

Code: Select all

#Persistent

CoordMode, ToolTip, Screen

Process, Exist, skype.exe
PID := errorLevel
; IfEqual, PID, 0, ExitApp
SetTimer, ShowToolTip 


ShowToolTip:
PrLoad := Round( GetProcessTimes(PID),2)
ToolTip %PrLoad%`%, 100,100
Sleep, 100

If (PrLoad > 9)
	{
	Process, Close, skype.exe
	}


GetProcessTimes( PID )    { 
   Static oldKrnlTime, oldUserTime 
   Static newKrnlTime, newUserTime 

   oldKrnlTime := newKrnlTime 
   oldUserTime := newUserTime 

   hProc := DllCall("OpenProcess", "Uint", 0x400, "int", 0, "Uint", pid) 
   DllCall("GetProcessTimes", "Uint", hProc, "int64P", CreationTime, "int64P"
           , ExitTime, "int64P", newKrnlTime, "int64P", newUserTime) 

   DllCall("CloseHandle", "Uint", hProc) 
Return (newKrnlTime-oldKrnlTime + newUserTime-oldUserTime)/10000000 * 100   
}
As You can see, it doesn't contain a loop at all and only one Return where it's actually needed. I have also tried looping the If statement together with parts before that, but the result is always the same - if script loads after process, everything works, if it's the other way around, it does not display the CPU percentage or kill the process. That's why I turned to this forum, because I ran out of logic here :) But I'm sure there exists a version of this script in the known universe where it defies all logic and works :)
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Process loading and script loading - who wins?

25 Sep 2018, 07:02

So what it should be is something like this

Code: Select all

#Persistent

CoordMode, ToolTip, Screen

Process, Exist, skype.exe
PID := errorLevel
; IfEqual, PID, 0, ExitApp
SetTimer, ShowToolTip 
Return ;Same with here, must have a return statement, unless you want it to run immediately 


ShowToolTip:
PrLoad := Round( GetProcessTimes(PID),2)
ToolTip %PrLoad%`%, 100,100
Sleep, 100

If (PrLoad > 9) {
	Process, Close, skype.exe
}
Return ;You have to have this return here otherwise it doesn't know where to end


GetProcessTimes( PID )    { 
   Static oldKrnlTime, oldUserTime 
   Static newKrnlTime, newUserTime 

   oldKrnlTime := newKrnlTime 
   oldUserTime := newUserTime 

   hProc := DllCall("OpenProcess", "Uint", 0x400, "int", 0, "Uint", pid) 
   DllCall("GetProcessTimes", "Uint", hProc, "int64P", CreationTime, "int64P"
           , ExitTime, "int64P", newKrnlTime, "int64P", newUserTime) 

   DllCall("CloseHandle", "Uint", hProc) 
Return (newKrnlTime-oldKrnlTime + newUserTime-oldUserTime)/10000000 * 100   
}
or with only a loop

Code: Select all

#Persistent
CoordMode, ToolTip, Screen
Loop {
	Process, Exist, skype.exe
	PID := errorLevel
	PrLoad := Round( GetProcessTimes(PID),2)
	ToolTip %PrLoad%`%, 100,100
	Sleep, 100

	If (PrLoad > 9) {
		Process, Close, skype.exe
		Break
	}
}
Return

GetProcessTimes( PID )    { 
   Static oldKrnlTime, oldUserTime 
   Static newKrnlTime, newUserTime 

   oldKrnlTime := newKrnlTime 
   oldUserTime := newUserTime 

   hProc := DllCall("OpenProcess", "Uint", 0x400, "int", 0, "Uint", pid) 
   DllCall("GetProcessTimes", "Uint", hProc, "int64P", CreationTime, "int64P"
           , ExitTime, "int64P", newKrnlTime, "int64P", newUserTime) 

   DllCall("CloseHandle", "Uint", hProc) 
Return (newKrnlTime-oldKrnlTime + newUserTime-oldUserTime)/10000000 * 100   
}
sethriel

Re: Process loading and script loading - who wins?

28 Sep 2018, 03:00

Thanks for the examples! And sorry for the delay, I couldn't find time to test the scripts. Luckily the second one with the loop works, however the first one doesn't. Now I have to figure out how not to kill the process while loading - during loading the load is of course high :) Probably with some kind of Wait after detecting the process. Anyway, have to learn about Returns and Breaks a bit more I guess. Thanks again and hopefully I can manage the rest now by myself :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Sem552, ShatterCoder and 146 guests