Timer stops when program enters a function Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
ferryiol

Timer stops when program enters a function  Topic is solved

12 Dec 2017, 18:31

just some code

roses = 1

settimer, timer1, 2500
timer1:
if such and such
roses = 1
if so and so
roses = 2
func5()
return

func5()
{
do some stuffs
loop
{
if roses = 1
sleep
if roses 2
break
}

not exactly the code but wanted to give you guys an idea, the thing is, whatever the status of roses was when the loop starts it seems to stay there and after tests i noticed the timer stops once it enters the function

is there a way to make timer run even if function is active?
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Timer stops when program enters a function

12 Dec 2017, 22:08

You should be getting the function to return. Then the timer can launch again. Or are you looking for multiple instances of the timer to be running simultaneously?

The best solution really depends on how the code is handled. But maybe you need not make your function loop? You could end up using another SetTimer to call your function.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Timer stops when program enters a function

13 Dec 2017, 00:57

In your example, your variable (roses) isn't globally defined, so that's probably the problem in your actual code, too. Either add:

Code: Select all

func5()
{
  global
or change your first declaration of roses, at the top, to:

Code: Select all

global roses = 1
Not doing either means that, when you use roses in the function, AHK treats it as a variable local to the function (i.e. a different roses than the first), so it's empty.
Noesis
Posts: 299
Joined: 26 Apr 2014, 07:57

Re: Timer stops when program enters a function

13 Dec 2017, 02:06

The major logic issue here is the loop in func5(), when roses = 1 (which it starts at) due to the fact func5 is also part of the timer. Since the function is part of the timer, the timer needs to complete itself before it can repeat. Since the function never completes it holds up the timer, because it was called from (i.e is part of) the timer.

FYI: Functions don't have to have a return statement in ahk they will auto return upon reaching their end (closing }). Also variable scope (i.e. globals or lack of) could easily be an issue too. The psuedo code you posted isn't exactly useful when it comes to working out if you've made an error, other than a logic error, since it is actually full of syntax errors and unknown comparisons, i.e. we kind of have to assume they work as you expect. There is also the issue here where the timer label is run on script start (i.e. auto executed) as there isn't a return between the setting of the timer and the label itself.

Anyway the only way to have the function run concurrently with the existing timer is to have it run in a different "thread", easiest method is via a separate timer.
ferryiol

Re: Timer stops when program enters a function

13 Dec 2017, 02:40

Osprey wrote:In your example, your variable (roses) isn't globally defined, so that's probably the problem in your actual code, too. Either add:

Code: Select all

func5()
{
  global
or change your first declaration of roses, at the top, to:

Code: Select all

global roses = 1
Not doing either means that, when you use roses in the function, AHK treats it as a variable local to the function (i.e. a different roses than the first), so it's empty.
that was the issue, it fixed it

Return to “Ask For Help”

Who is online

Users browsing this forum: blad4, mikeyww, RubbeH and 53 guests