Jump to content

Stop Loop immediately


sajgon
 Share

Recommended Posts

Hi. Can anyone tell me how can i stop loop in autoit. I know how to pause and resume but what i need is to stop... for example

CODE
#include <GuiConstants.au3>

global $Paused

hotkeyset("{numpadadd}","quit")

hotkeyset("{numpad0}","stop")

hotkeyset("{numpad1}","start")

guicreate("test",300,300,300,300)

$play = guictrlcreatebutton("START",120,120,60,60)

guisetstate()

While 1

$msg = GuiGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $play

$loop = 0

Do

mousemove(100,500)

sleep(200)

mousemove(200,500)

sleep(200)

mousemove(300,500)

sleep(200)

mousemove(400,500)

sleep(200)

mousemove(500,500)

sleep(200)

until $loop = 1

EndSelect

WEnd

func quit()

exit 0

EndFunc

func stop()

WHAT SHOULD GO HERE?

EndFunc

func start()

$msg = $play

EndFunc

When i'm in the loop and use pausing it stops but when i resume it is still in the loop. I need function which IMMEDIATELY stop the loop(go back too moment when no start key was pressed or pause it in that way that after resuming i starts from the beginning of loop not form the moment where it was stoped. Like:

BAD: mousemove(100,500)----------------mousemove(200,500)--pause()..........go to toilet and back.............resume()---mousemove(300,500)----- etc.

GOOD(1st option): mousemove(100,500)----------------mousemove(200,500)--stop(): go back to moment where script was freshly opened and ready for orders(just like when i pressed F5 button in script editor)

GOOD(2nd option): mousemove(100,500)----------------mousemove(200,500)-----pause()...........go to toilet nad back................resume(): play from the beginning(NOT THE PLACE WHERE IT WAS STOPPED) of loop which is: mousemove(100,500)--------- etc.

Hope that someone understan my engllish xD

Edited by sajgon
Link to comment
Share on other sites

In your function stop have something like this:

Func stop()
$going = False
EndFunc

$going should be declared at the top as true like this:

$going = True

Then, in your loop, do something like this:

Do
;rest of loop
Until Not $going

Since your do loop has lots of sleep you can just put a condition in between each line like this:

mousemove(100,500)
if not $going then exitloop
sleep(200)
mousemove(200,500)
if not $going then exitloop
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

It is a solution but...:) This is only example script and it is very short. Script where i want to use this stop function is much longer and puting "if not $going then exitloop" beetwen every line will be very hard:P

Anyway i've already made a solution for my 2nd good option. Now i need 1st good option get to work. For now only working solution is to restart whole script like:

(after compiling)

func stop()

run("test.exe")

exit 0

endfunc

It runs new process so it is freshly opened and at the same time close the old one -> get a new one and get rid of old;)

But i think there have to be another way...

Link to comment
Share on other sites

Link to comment
Share on other sites

Then you'll either have to change the way the loop is executed or have multiple tests within the loop.

It is easier to change the loop.

#include <GuiConstants.au3>
global $Paused
hotkeyset("{numpadadd}","quit")
hotkeyset("{numpad0}","stop")
hotkeyset("{numpad1}","start")
guicreate("test",300,300,300,300)
$play = guictrlcreatebutton("START",120,120,60,60)
guisetstate()

While 1
$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $play
$loop = 0
Do
    For $x = 1 to 5
                mousemove($x*100,500)
                If $loop Then ExitLoop
                sleep(200)
    Next
until $loop = 1
EndSelect
WEnd

func quit()
exit 0
EndFunc

func stop()
$loop = 1
EndFunc

func start()
$msg = $play
EndFunc
Edited by eltorro
Link to comment
Share on other sites

I see but as i said this is only example script and my script is much more complicated so changing loop in that way wont be possible. Loop have different functions inside like pixelgetcolors, mousemoves, sends, ifs etc.

So you are saying that the only way is to put multiple tests inside the loop? Its wierd that autoit dont have such a simple function as "stop everything right now". So exiting the script and reopening it is only solution for me?

Edited by sajgon
Link to comment
Share on other sites

I see but as i said this is only example script and my script is much more complicated so changing loop in that way wont be possible. Loop have different functions inside like pixelgetcolors, mousemoves, sends, ifs etc.

So you are saying that the only way is to put multiple tests inside the loop? Its wierd that autoit dont have such a simple function as "stop everything right now". So exiting the script and reopening it is only solution for me?

Without seeing the actual script... Yes. Either multiple tests or kill the script and start over.

Link to comment
Share on other sites

HI,

you can try to use AdlibEnable to check for the loop breaking condition every X ms or use a hotkey.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

I dont know if i understand this adlibenable function as it should be understan but i made this:

CODE
#include <GuiConstants.au3>

$going = true

global $Paused

hotkeyset("{numpadadd}","quit")

hotkeyset("{numpad0}","stop")

hotkeyset("{numpad1}","start")

guicreate("test",300,300,300,300)

$play = guictrlcreatebutton("START",120,120,60,60)

guisetstate()

While 1

$msg = GuiGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $play

start()

EndSelect

WEnd

func quit()

exit 0

EndFunc

Func stop()

AdlibEnable("going",1)

EndFunc

func going()

$going = False

EndFunc

func start()

$going = true

AdlibDisable()

Do

mousemove(100,500,0)

sleep(1000)

mousemove(200,500,0)

sleep(1000)

mousemove(300,500,0)

sleep(1000)

mousemove(400,500,0)

sleep(1000)

mousemove(500,500,0)

sleep(1000)

until not $going

EndFunc

And it still stops the loop at the end, not at the moment when i pressed hotkey. If i'm doing something wrong please correct me.

For now the only method which worked are multiple tests.

Link to comment
Share on other sites

I dont know if i understand this adlibenable function as it should be understan but i made this:

CODE
#include <GuiConstants.au3>

$going = true

global $Paused

hotkeyset("{numpadadd}","quit")

hotkeyset("{numpad0}","stop")

hotkeyset("{numpad1}","start")

guicreate("test",300,300,300,300)

$play = guictrlcreatebutton("START",120,120,60,60)

guisetstate()

While 1

$msg = GuiGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $play

start()

EndSelect

WEnd

func quit()

exit 0

EndFunc

Func stop()

AdlibEnable("going",1)

EndFunc

func going()

$going = False

EndFunc

func start()

$going = true

AdlibDisable()

Do

mousemove(100,500,0)

sleep(1000)

mousemove(200,500,0)

sleep(1000)

mousemove(300,500,0)

sleep(1000)

mousemove(400,500,0)

sleep(1000)

mousemove(500,500,0)

sleep(1000)

until not $going

EndFunc

And it still stops the loop at the end, not at the moment when i pressed hotkey. If i'm doing something wrong please correct me.

For now the only method which worked are multiple tests.

You have to check for "$going" just like in the previous script checking for "$loop".

Without the whole script, it not possible to second guess you and suggest a more efficient solution here.

You've got a hotkey that sets the condition $loop (previous script.) to 1, Test for it.

Using AdlibEnable, put your loop into a separate function.

#include <GuiConstants.au3>
Global $going
HotKeySet("{numpadadd}", "quit")
HotKeySet("{numpad0}", "stop")
HotKeySet("{numpad1}", "start")
GUICreate("test", 300, 300, 300, 300)
$play = GUICtrlCreateButton("START", 120, 120, 60, 60)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $play
            start()
    EndSelect
WEnd
Func quit()
    Exit 0
EndFunc   ;==>quit
Func stop()
    $going = False
    AdlibDisable()
EndFunc   ;==>stop
Func start()
    AdlibEnable("MyTask", 100)
    $going = True
EndFunc   ;==>start
Func MyTask()
    MouseMove(100, 500, 0)
    If Not $going Then Return
    Sleep(1000)
    MouseMove(200, 500, 0)
    If Not $going Then Return
    Sleep(1000)
    MouseMove(300, 500, 0)
    If Not $going Then Return
    Sleep(1000)
    MouseMove(400, 500, 0)
    If Not $going Then Return
    Sleep(1000)
    MouseMove(500, 500, 0)
    If Not $going Then Return
    Sleep(1000)
EndFunc   ;==>MyTask

Of course if won't stop until after the "sleep" has finished.

Link to comment
Share on other sites

I see that there are different ways to do this but every option need to do multiple tests. I've already used method of Piano Man. I also put my loop into seperate function. It took me some time but now it works just as i want to. Thaks for every post in this topic:)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...