SetTimer & function calls, I need help finding the errors

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
micnolmad
Posts: 5
Joined: 19 Jan 2018, 10:47

SetTimer & function calls, I need help finding the errors

20 Oct 2018, 10:03

Hi, I am trying to make a script that can be set to run and continuously check if a process exists.

If it does, start a timer to close it after some time.

I have read some manual and forum solutions and adapted what I thought was right but I just can't get it to work.

As of now I don't get any errors, it simply just runs and closes.

Code: Select all

V_Process := "ExecPubg.exe"

ProcessExist(Name){
	Process,Exist,%Name%
	if (ErrorLevel > 0){
		SetTimer,L_ProcessClose,-120000
		SetTimer,L_ProcessExist,Delete
	}
}

ProcessClose(Name){
	Process,Close,%Name%
}

SetTimer,L_ProcessExist,1000

L_ProcessExist:
ProcessExist(V_Process)

L_ProcessClose:
ProcessClose(V_Process)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: SetTimer & function calls, I need help finding the errors

20 Oct 2018, 10:19

the error is the labels lack a return statement, resulting in the following:

Code: Select all

V_Process := "ExecPubg.exe" ; this gets evaluated

ProcessExist(Name){
	Process,Exist,%Name%
	if (ErrorLevel > 0){
		SetTimer,L_ProcessClose,-120000
		SetTimer,L_ProcessExist,Delete
	}
}

ProcessClose(Name){
	Process,Close,%Name%
}

SetTimer,L_ProcessExist,1000 ; this gets evaluated, timer routine will run 1 second from now
; no return here, so keep going

L_ProcessExist: ; this gets evaluated
ProcessExist(V_Process) ; this gets evaluated
; no return here either, so keep going

L_ProcessClose: ; this gets evaluated
ProcessClose(V_Process) ; this gets evaluated
; no return here either. script isnt marked persistent and contains no hotkeys/strings, script is done. exit
and also lack of #Persistent or hotkeys, if thats ur entire script
digidings
Posts: 24
Joined: 22 Jan 2018, 17:04

Re: SetTimer & function calls, I need help finding the errors

20 Oct 2018, 10:28

Your subroutine L_ProcessExist also executes L_ProcessClose, because of the missing return statement.

Try this:

Code: Select all

global CloseAfter_ms := 120000 ; 120 seconds
V_Process := "ExecPubg.exe"

ProcessExist(Name){
    Process,Exist,%Name%
    if (ErrorLevel > 0){
        SetTimer,L_ProcessClose,% -CloseAfter_ms
        SetTimer,L_ProcessExist,Delete
    }
}

ProcessClose(Name) {
    Notify("ProcessClose(" . Name . ")")
    Process,Close,%Name%
}

SetTimer,L_ProcessExist,1000

L_ProcessExist:
    ProcessExist(V_Process)
    return

L_ProcessClose:
    ProcessClose(V_Process)
    return
micnolmad
Posts: 5
Joined: 19 Jan 2018, 10:47

Re: SetTimer & function calls, I need help finding the errors

20 Oct 2018, 17:48

Awesome, I added #Persistent which made it script not close at once. I also added a return to both functions. They dont return anything.

Next problem is that as soon at the script sees the process it is closed instead of waiting the -120000.

Also I added a SetTimer,,Delete for the Close function but really I just want the script to end/exit at that point. Close it self. Can it do that now that it is #Persistent?

Code: Select all

#Persistent

V_Process := "ExecPubg.exe"

ProcessExist(Name){
	Process,Exist,%Name%
	if (ErrorLevel > 0){
		SetTimer,L_ProcessClose,-120000
		SetTimer,L_ProcessExist,Delete
	}
	return
}

ProcessClose(Name){
	Process,Close,%Name%
	SetTimer,L_ProcessClose,Delete
	return
}

SetTimer,L_ProcessExist,1000

L_ProcessExist:
ProcessExist(V_Process)

L_ProcessClose:
ProcessClose(V_Process)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: SetTimer & function calls, I need help finding the errors

20 Oct 2018, 18:36

Code: Select all

#Persistent

V_Process := "ExecPubg.exe"

ProcessExist(Name){
	Process,Exist,%Name%
	if (ErrorLevel > 0){
		SetTimer,L_ProcessClose,-120000
		SetTimer,L_ProcessExist,Delete
	}
}

ProcessClose(Name){
	Process,Close,%Name%
	; SetTimer,L_ProcessClose,Delete ; not needed
	ExitApp
}

SetTimer,L_ProcessExist,1000
Return ; end auto-execute section

L_ProcessExist:
ProcessExist(V_Process)
Return ; end label

L_ProcessClose:
ProcessClose(V_Process)
Return ; end label, but its superfluous since nothing follows
micnolmad
Posts: 5
Joined: 19 Jan 2018, 10:47

Re: SetTimer & function calls, I need help finding the errors

23 Oct 2018, 09:35

The returns helped the timers work.

The script doesn't exit though. Should I use Exit instead of Return for the last label?

To answer my question, I used ExitApp at the end and now it works.

Code: Select all

#Persistent

V_Process := "ExecPubg.exe"

ProcessExist(Name){
	Process,Exist,%Name%
	if (ErrorLevel > 0){
		SetTimer,L_ProcessClose,-60000
		SetTimer,L_ProcessExist,Delete
	}
	return
}

ProcessClose(Name){
	Process,Close,%Name%
	SetTimer,L_ProcessClose,Delete
	return
}

SetTimer,L_ProcessExist,1000
return

L_ProcessExist:
ProcessExist(V_Process)
return

L_ProcessClose:
ProcessClose(V_Process)
exitapp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot], Spawnova and 340 guests