Jump to content

Exitloop using an hotkey


Recommended Posts

Hi,

How can I exit/break a loop using an Hotkey?

I tried this but the only problem is that the expression is tested after the loop is executed and I want something that will break the loop at anytime not only after the expression.

Actually I dont want to Exit my script but ONLY exit the loop.

HotKeySet("{ESC}", "_Stop")

Do
;blabla
;blabla
; +- 1000 lines of script between the Do ... Until
Until $Stop = 1

Func _Stop()
    $Stop = 1
EndFunc
Edited by Dieuz
Link to comment
Share on other sites

just use this example, it shows exactly how to pause or exit

.... straight from help at the bottom of the page ( thats where you get the HotKeySet() function

; Press Esc to terminate script, Pause/Break to "pause"

Global  $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}",  "Terminate")
HotKeySet("+!d", "ShowMessage")  ;Shift-Alt-d

;;;; Body  of program would go here ;;;;
While 1
     Sleep(100)
WEnd
;;;;;;;;

Func TogglePause()
    $Paused = NOT  $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is  "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func  Terminate()
    Exit 0
EndFunc

Func ShowMessage()
     MsgBox(4096,"","This is a message.")
EndFunc

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks but that doesnt help me because there's nothing in there to "break" a loop without exiting the current script.

The "Pause" function stop the loop but doesnt exit it... I'm looking for something that will exit the loop at anytime. Maybe I could add something in the Pause function to break my current loop but what should I add?

Edited by Dieuz
Link to comment
Share on other sites

Your code is working ... if you declare the $Stop variable first.

You might consider the example Valuater gave you because it shows something extra :whistle:

HotKeySet("{ESC}", "_Stop")
Global $Stop = 0
Do
;blabla
;blabla
; +- 1000 lines of script between the Do ... Until
Until $Stop = 1

Func _Stop()
    $Stop = 1
EndFunc

this is the working code :lmao:

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Your code is working ... if you declare the $Stop variable first.

You might consider the example Valuater gave you because it shows something extra :whistle:

HotKeySet("{ESC}", "_Stop")
Global $Stop = 0
Do
;blabla
;blabla
; +- 1000 lines of script between the Do ... Until
Until $Stop = 1

Func _Stop()
    $Stop = 1
EndFunc

this is the working code :lmao:

... and if you want to exit the loop in the middle (rather than at the end of the loop), just test $Stop at various points in the loop, exiting if stop is true.
Link to comment
Share on other sites

Your code is working ... if you declare the $Stop variable first.

I just forgot to declare it in this example :whistle:

... and if you want to exit the loop in the middle (rather than at the end of the loop), just test $Stop at various points in the loop, exiting if stop is true.

I thought of that but I dont want to put If statement everywhere. There's really no such things in autoit to stop a loop in the middle rather than at the end?

Link to comment
Share on other sites

AdLibEnable an If $stop = 1 Then ExitLoop statement...

Edit: Don't try that...

Edited by mikehunt114
IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

There's really no such things in autoit to stop a loop in the middle rather than at the end?

Can you give an example in any non-multi-threading language where you can do what you want?

Valuater's method lets you pause the loop, but if you want to exit the loop at any time and then continue with other parts of the script then I think

1) Are you really sure you need to do this or can't think of a way round it?

2) If you must have this feature then you could put the loop in another script and run that from your main script, then you could kill it at any time. You could pass it any parameters you wanted or use pipes.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I thought of that but I dont want to put If statement everywhere. There's really no such things in autoit to stop a loop in the middle rather than at the end?

there is no need for such a thing, unless you can prove I'm wrong.

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

there is no need for such a thing, unless you can prove I'm wrong.

Well Im using a Gui and when the user click on a button, it began to make scheduled task and the program control the mouse. When the mouse is controled by the program, you cant really move it by yourself because it will all screw up. It's why I need an hotkey on the keyboard to stop that because the user cant click on a stop button. The tasks took habitually 1-2 minutes and it's why I cant wait for those to finish. I want that when the user want to, he can break that tasks loop. When he will do so, a picture will appear on the gui to show that the task are stopped.

If anyone see a workaround, let me know but I think that the Exitloop using an hotkey is the best solution...

Link to comment
Share on other sites

Simply set your HotKey function to show that you are exiting the loop, that's all. Your HotKey function doesn't have to do anything spectacular, just SplashText, or MsgBox, or whatever you like. No need to exit your loop like suggested, a simple HotKey with the proper function (including an Exit at the end) will do the job well.

Edited by mikehunt114
IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

I've read through this thread more then once and I'm more then a little unclear. Perhaps you should define exactly what you mean by EXIT Loop, especially where you said exiting the loop in the middle instead of the end. Exiting the loop totally breaks the loop and does not start back up (without coding for this) where the loop left off. If you want to retain where you left off you are either; coding a 'start' position, or you are pausing the loop.

Since you are talking about using a GUI, you already have a loop for that. Enclose your code(the second loop) in a Func/Endfunc with any of the methods for stopping above. Use Return to set an error if stopped, and have your GUI loop read the returned @error and display that pic you were talking about earlier.

If you are concerned with having your code remember the loops position when exiting thats totally different....

At any rate I could be totally wrong because I'm still not sure what your talking about... so.. sorry if I am.

Edited by someone
While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

If you just set up your script correctly, you won't have any issues. You don't need to exit a loop in the middle...HotKeySet a function to start a new loop, or pause the first one, or do any of many things...and do whatever you want to do there (totally separate from your first loop). Like Someone suggested, set your script up with functions to make things easy. If you need a clearer explanation, post your code, and your goal, and someone here will help you.

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

Since you are talking about using a GUI, you already have a loop for that. Enclose your code(the second loop) in a Func/Endfunc with any of the methods for stopping above. Use Return to set an error if stopped, and have your GUI loop read the returned @error and display that pic you were talking about earlier.

The problem is it will display the pic but it wont break the loop in the function.

with any of the methods for stopping above

The problem is that I dont have any "working" stopping method

This is what Im talking about, How can I break this loop using an hotkey?

HotKeySet("{ESC}", "_Stop")


Traytip("", "Loop puzzle is running", 2000)

Looptime()

Func Looptime()
While 1
sleep(100) ; this is equal to a very big loop, I mean that this part represent +-1000 lines of script.
Wend

MsgBox(0,"HURRAY", "You found the solution!")
Exit
EndFunc


Func _Stop()
;;;;; What to write here ??
EndFunc
Link to comment
Share on other sites

Ok nm, that didn't quite work the way i expected.

Edit: ohh u could make the loop part a separate script and have your main script call it, then the func _stop cound be exit.

Edited by sccrstvn93
Link to comment
Share on other sites

Edit: ohh u could make the loop part a separate script and have your main script call it, then the func _stop cound be exit.

Yeah that make sense but is it possible to make two .au3 file only one .exe when compiled?

Anyway, I am sure there's a way using only one script but how?

Edited by Dieuz
Link to comment
Share on other sites

the only other way i can think of is to monitor a variable before every line of coed in the loop, and then use a function to change that variable.

;;;;;Code
If $stop = 1 then
Exit
Endif
;;;;code
If $stop = 1 then
Exit
Endif



Func _stop
$stop = 1
Endfunc
Edited by sccrstvn93
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...