0

This should be easy! I've checked out this thread, but it doesn't help.

I can't seem to break out of the loop using the hotkey. The idea is that you can start and stop the looping process with a single hotkey.

It seems like the value of timeron never gets into the the loop once it's begun.

Here is a sample script:

#singleinstance force

timeron = 0
return

!f7::
    if(timeron){
        timeron = 0
        msgbox Okay, the loop is off.
    }else{
        timeron = 1 ;if this is not set to one, the loop will not begin
        msgbox Turning on the loop.
        gosub, STARTLOOPING
    }
RETURN


STARTLOOPING:
    ;do this over and over
    loop{
        if(!timeron)
            break   
        ;now wait for the right length of time before continuing the loop
        msgbox, The loop yet continues....
        Sleep, 5000
        if(!timeron)
            break
    }
RETURN

What am I missing here?

Community
  • 1
  • 1
bgmCoder
  • 6,205
  • 8
  • 58
  • 105

2 Answers2

3

Since your !F7 never ends, a second press of !F7 is ignored. Per default there is only one thread for each hotkey allowed at one time.

Add

#MaxThreadsPerHotkey 2

as a second line to your script then the second !F7 press can deactivate the loop.

576i
  • 7,579
  • 12
  • 55
  • 92
  • I figured there was a line I could add to affect the threads - I searched through the docs and the answer was not readily apparent to me! Thanks for that! – bgmCoder Nov 23 '13 at 14:27
2

Why don't you use timers instead? They allow your script to do other stuff in between timer runs, thus allowing hotkeys to interrupt them:

timeron := false
Exit
!F7::
    if(timeron) {
        timeron := false
        SetTimer, MyTimer, Off
    } else {
        timeron := true
        ; Call GoSub once if you want the subroutine
        ; to fire immediately at the beginning
        GoSub, MyTimer
        ; Then let the timer repeat it
        SetTimer, MyTimer, 5000
    }
return

MyTimer:
    Msgbox, Looping like crazy!
return

You can always replace a loop's functionality with a timer. If you have some kind of for loop/counter, you can use global variables instead.

MCL
  • 3,985
  • 3
  • 27
  • 39
  • MCL this is a good answer. I marked the other one, however, because it worked without my having to edit the script too much. But I will think about this approach, too. Thanks! – bgmCoder Nov 23 '13 at 14:28
  • Actually, I think I will use this in my script. – bgmCoder Nov 23 '13 at 14:38
  • I agree that MCL's answer is better - this is how I would have programmed the loop myself... My answer was just a direct answer to your question, not an attempt to optimize your code beyond that. – 576i Nov 26 '13 at 09:38