Sign in to follow this  
Followers 0
Kradon

ExitLoop not exitting

16 posts in this topic

Hi, heres the code I'm using..

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author: Forsaken
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>

GUICreate("Auto", 335, 100)

GUISetState(@SW_SHOW)

GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)

$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)

While 1
    $msg = GUIGetMsg()
    
    Select
        
        Case $msg = $startbutton
                
                $send1 = GUICtrlRead($key1)
                $sleep1 = GUICtrlRead($time1)
                
            While 1    
                Send($send1)
                Sleep($sleep1)
            WEnd
            
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
            
    EndSelect
        
WEnd

when i press "start" which will run the code inside the selection and then press the "X" button which is supposed to exit the loop and close the GUI..but it does not.

the "X" button only works when i didnt start the loop and just closed it right after opening it.

i'm guessing the "sleep" function paused the script so that when i press the X button the program did not read it. is there any solution to this?

also if possible, i would like a stop button so it'll only exit the loop and do nothing until "start" is pressed again and by doing that will start the script from the beginning.

thanks.

Share this post


Link to post
Share on other sites



thats what i thought it did, is there any way around it?

Share this post


Link to post
Share on other sites

thanks johnone, that helped, but is there a way to stop a script, not pause because pausing would pause the script where it left off, i want the script to be stopped and resume at the beginning on a press of a hotkey since thats the only way around being stuck in the loop.

thanks!

Share this post


Link to post
Share on other sites

Implement the following into your code

Global $ExitLoop = False ; A global variable to hold state

HotKeySet("{ESC}","_ExitLoop") ; Make a function to change state to true

Func _ExitLoop() ; Make a function to change state to true
    $ExitLoop = True
EndFunc

$ExitLoop = False ; Before entering your endless loop, set state to false
While 1
    If Not $ExitLoop Then ; Only Send if state is false
        Send($send1)
        Sleep($sleep1)
    Else 
        ExitLoop ; Else exit the loop
    EndIf
WEnd

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Kradon,

Your script will not respond well (if at all) if you have a loop/sleep within the message loop. Also, your

guisetstate() should be after you define your controls.

Lastly, sleep is in millisecs so you probably want to multiply the value from your time control by 1000 to get seconds.

The following will "send" a key on a second interval defined by $time1. The code is rough and there is no input validation, but, hell, you're bored anyway...

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author: Forsaken
#ce ----------------------------------------------------------------------------


#include <GUIConstantsEx.au3>

GUICreate("Auto", 335, 100)

GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
guisetstate()


local $start = false

While 1
    $msg = GUIGetMsg()

    Select

        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit

        Case $msg = $startbutton
            adlibregister('_sender',guictrlread($time1)*1000)

    EndSelect

WEnd

func _sender()

    send(guictrlread($key1))
    ConsoleWrite('Sending ' & guictrlread($key1) & @LF)

endfunc

kylomas

edit: additional info - I almost forgot - you delete your gui and then do exitloop. this leaves your script "hanging". Use "exit" to exit the script.

Edited by kylomas
1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

thanks, it works the only problem is when i will have to set $exitloop back to false in order to start the script again. i created a hotkey to make it go back to false but would it be possible to make it so ESC hotkey can set it to true to exit loop then back to false?

EDIT: kylomas, your script works perfectly, and thanks for the tips will definitely keep this in mind.

thanks everyone for helping!

Edited by Kradon

Share this post


Link to post
Share on other sites

JohnOne is right, i havent been able to think of a way to stop the function without closing the script.

Share this post


Link to post
Share on other sites

What is wrong with the implementation I posted.

That will set $ExitLoop to false every time you hit the start button, as soon as you hit the hotkey it should stop sending, exit the loop, keep your script going and the gui should respond.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Of course, just toggle on the start buttonor add a stop button to stop it. J1's solution probably works also, assuming you've corrected the other problems.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Here is a different fahion

#include <GUIConstantsEx.au3>

HotKeySet("{ESC}", "_ExitLoop") ; Make a function to change state to true

Global $ExitLoop = False ; A global variable to hold state

Func _ExitLoop() ; Make a function to change state to true
    $ExitLoop = True
EndFunc   ;==>_ExitLoop

GUICreate("Auto", 335, 100)

GUISetState(@SW_SHOW)

GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)

$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)

While 1
    Switch GUIGetMsg()
        Case $startbutton
            $send1 = GUICtrlRead($key1)
            $sleep1 = GUICtrlRead($time1)
            $ExitLoop = False ; Before entering your endless loop, set state to false
            While Not $ExitLoop; Only Send if state is false
                Sleep($sleep1)
                Send($send1)
            WEnd
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
    EndSwitch
WEnd
1 person likes this

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Like this

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author: Forsaken
#ce ----------------------------------------------------------------------------


#include <GUIConstantsEx.au3>

GUICreate("Auto", 335, 100)

GUICtrlCreateLabel("Key", 8, 10)
$key1 = GUICtrlCreateInput("", 35, 8, 120)
GUICtrlCreateLabel("Time", 8, 44)
$time1 = GUICtrlCreateInput("", 35, 40, 120)
$startbutton = GUICtrlCreateButton("Start", 190, 8, 60)
guisetstate()


local $start = false

While 1
    $msg = GUIGetMsg()

    Select

        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit

        Case $msg = $startbutton
            if guictrlread($startbutton) = 'Start' then
                adlibregister('_sender',guictrlread($time1)*1000)
                guictrlsetdata($startbutton,'Stop')
            else
                adlibunregister('_sender')
                guictrlsetdata($startbutton,'Start')
            endif

    EndSelect

WEnd

func _sender()

    send(guictrlread($key1))
    ConsoleWrite('Sending ' & guictrlread($key1) & @LF)

endfunc

kylomas

1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

wow, both scripts work thanks you two.

Share this post


Link to post
Share on other sites

Kradon,

Take away from this:

"Lot's of way to skin the cat but you always start from the outside."

Meaning - Some things are fundamental to the language and vary only through implementation.

Good Luck,

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0