 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Tyrsius
Joined: 09 Jul 2009 Posts: 45
|
Posted: Wed Sep 30, 2009 2:34 am Post subject: Using a GUI in the middle of execution |
|
|
I am working on a script that currently interrupts the flow of execution with a message box, allowing the user a chance to make some modifications to the window be manipulated, as well as answer yes or no to a question from the script. However, msgbox does not have a lot of the control we need so we have decided to try to use a GUI instead.
My problem is getting back into the flow of execution. How do you do it? The Returns needed at the end of the labels inside the GUI, as well as the return at the of the guiclose stop the script from continuing. The only method I have can come up with for getting around this is to place a label where execution needs to pick up again, and use a goto before the returns in each label.
I have always been wary of using gosubs and gotos though, at the warning of others. Is there another way to return to the thread that the GUI has interrupted, or is goto the solution? |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 882 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Wed Sep 30, 2009 3:07 am Post subject: |
|
|
You should use GoSub, there is no problem at all.
I always avoid GoTo. _________________
Antonio França
aka MasterFocus aka Tunis
+ My AHK stuff: ~MasterFocus
+ AHK @ irc.freenode.net: #ahk
Contact: PM only ! |
|
| Back to top |
|
 |
Tyrsius
Joined: 09 Jul 2009 Posts: 45
|
Posted: Wed Sep 30, 2009 3:16 am Post subject: |
|
|
I Guess I forgot to say that the thread is a loop. A Gosub would stop it from looping properly, wouldn't it?
After some testing though, goto also stops it from using "continue" properly. I might have to abandon this idea of a GUI inside of a loop. |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 882 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Wed Sep 30, 2009 3:19 am Post subject: |
|
|
Maybe use SetTimer instead of Loop? (hard to know without seeing your code)
Or abandon the Loop and split it into two Labels: one executed before the GUI, one after. _________________
Antonio França
aka MasterFocus aka Tunis
+ My AHK stuff: ~MasterFocus
+ AHK @ irc.freenode.net: #ahk
Contact: PM only ! |
|
| Back to top |
|
 |
Tyrsius
Joined: 09 Jul 2009 Posts: 45
|
Posted: Wed Sep 30, 2009 3:26 am Post subject: |
|
|
| As a side question, why is gosub better than goto? |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Sep 30, 2009 3:30 am Post subject: |
|
|
I too have had this problem which admittedly is very specific.
It's because the topology of your program flow isn't right to begin with. Post your code and where you would want the GUI and I or someone can try to show you how to better organize it, I bet you aren't missing much. |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 882 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Wed Sep 30, 2009 3:33 am Post subject: |
|
|
It is hard for me to try to explain this in english, but I quickly Google'd and found this. Basically, Goto "breaks the flow". Gosub needs a Return, so it can be interpreted as a block of commands (or even a function) that is inserted at certain point, which doesn't compromise the natural flow of the code.
It's like reading a book. Goto tells you to jump to a certain page and don't go back, which is kinda messed up.
Gosub tells you to read a certain page before proceeding from where you stopped. _________________
Antonio França
aka MasterFocus aka Tunis
+ My AHK stuff: ~MasterFocus
+ AHK @ irc.freenode.net: #ahk
Contact: PM only ! |
|
| Back to top |
|
 |
tidbit
Joined: 09 Mar 2008 Posts: 649 Location: USA
|
Posted: Wed Sep 30, 2009 3:38 am Post subject: |
|
|
goto goes to the section and doesn't go back. gosub goes back to where it left off.
example (from what I understand. may not be 100%):
A is called. B never gets called. it skips and goes to C.
A is called. C gets called and then goes back to where it left off and then B gets called. _________________ rawr. be very affraid
*poke*
Note: My name is all lowercase for a reason. |
|
| Back to top |
|
 |
randallf
Joined: 06 Jul 2009 Posts: 394
|
Posted: Wed Sep 30, 2009 3:45 am Post subject: |
|
|
I looked at some of my stuff and when using GUI's in the middle of routines I use a unique GUI and do this: (edited to give a more complete topology of a program that has an initial menu and additional GUI's afterward)
Simply put, I try to have the program 'in neutral' and operate off of the button subroutines.
| Code: | ;Autoexecute functions like
#SingleInstance FORCE
#SetKeyDelay 0
;initial GUI commands go here
Return
SubRoutine: ;routine fired from GUI button
IF x = y
{
;...commands
Gui 4: Destroy
GoSub, GuiMenu4
;... commands
} ;if x = y loop end
Return ;SubRoutine return
;...other subroutines
GuiMenu4:
Gui 4:Add, Button, Text
Gui 4:Show
Return
|
Then it doesn't matter when the loop is called or what it's in since the gui is destroyed prior to execution, as long as you don't plan to leave that GUI up (i.e. it is an input or messagebox of some type that doesn't continuously display, probably most things are in this category)
You may sometimes need to use PAUSE ON/OFF as well.
Edited a few times, check again |
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 1158 Location: San Diego, California
|
Posted: Wed Sep 30, 2009 6:17 am Post subject: |
|
|
Well, here's my info/advice on the subject ... first gosub vs goto
Here are my working definitions of the two.
Goto - to transfer execution from one part of code to a different part of the code, with no regard (thought) of returning to that 'from' point.
Gosub - to transfer execution from one part of code to a different part of the code, with purposeful intent (thought) of returning to and continuing execution at that 'from' point.
Here is an example program. First, read through the listing. Then run it without change.
Then change the first gosub to goto and run it again. Do you see the difference?
| Code: |
msgbox, , , A program to help explain the difference between 'gosub' and 'goto', 3
; after running it, change the first 'gosub' to 'goto' and run it again.
a= This is a test
t= 2
gosub, show_it
a= How's it going?
t= 2
gosub, show_it
a= What time it?
t= 2
gosub, show_it
msgbox, , , Program ending, 2
exitapp
show_it:
msgbox, , , %a% , %t%
return
|
And here is a working (albeit contrived) script with a loop and a gui in the middle of the loop.
The gui could be more complicated, but at least this explains the process.
The msgboxs and the sleep, 1000 are for the demo.
I'd leave the sleep, 100 intact because it prevents the winwaitnotactive from running before gui, show is finished displaying the gui.
| Code: | ; --------- create the gui before starting the loop
Gui, Add, Button , w150 Default, OK
gui, add, text, w150 vloopcnt, before a_index =
gui, add, text, w150 vloopcnt2, after a_index =
gui, add, text, w150 , Press OK to continue
loop, 10 ; ------------------- this is the start of the loop
{
msgbox,, ,starting with the loop %a_index%`n`n(timed msgbox), 2
sleep, 1000
; you can update the gui before/after
; ===================================
guicontrol,, loopcnt, before a_index = %a_index% ; --- update the gui before showing
gui, show, w200 , please hold ; --- show the gui
sleep, 1000
guicontrol,, loopcnt2, after a_index = %a_index% ; --- or update the gui after showing
sleep, 100
winwaitnotactive, please hold ; put the loop on hold, while running the gui
sleep, 1000
msgbox,, ,continue with the loop %a_index%`n`ntimed msgbox, 2
sleep, 1000
} ; ------------------- this is the end of the loop
msgbox end of the script - `n`npress OK to exit ...
exitapp
buttonOK:
gui, hide
return |
Hope this helps.
Leef_me |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|