AutoHotkey Community

It is currently May 26th, 2012, 11:54 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: September 30th, 2009, 3:34 am 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:07 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
You should use GoSub, there is no problem at all.
I always avoid GoTo.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:16 am 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:19 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
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.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:26 am 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
As a side question, why is gosub better than goto?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:30 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:33 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
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.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:38 am 
Online

Joined: March 10th, 2008, 12:55 am
Posts: 1907
Location: Minnesota, USA
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%):
Code:
A
goto, C
B
.
C

A is called. B never gets called. it skips and goes to C.
Code:
A
gosub, C
B
.
C

A is called. C gets called and then goes back to where it left off and then B gets called.

_________________
rawr. be very afraid
*poke*
Note: My name is all lowercase for a reason.
"I think Bigfoot is blurry, that's the problem. It's not the photographer's fault, Bigfoot is blurry. So there's a large, out-of-focus monster roaming the countryside."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 4:45 am 
Offline

Joined: July 6th, 2009, 9:58 pm
Posts: 678
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 7:17 am 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6068
Location: San Diego, California
Well, here's my info/advice on the subject ... first gosub vs goto
:arrow: 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.

:arrow: 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.
:arrow: 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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot], hyper_, Leef_me, patgenn123, Pulover, RaptorX, rbrtryn, XstatyK, Yahoo [Bot] and 20 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Group