Jump to content
Sign in to follow this  
DaProgrammer

Stopping a For loop By pressing a Gui Button [Solved]

Recommended Posts

DaProgrammer

Func Func()
      For $i = 1 to 10
          Sleep(1000)
      Next
  EndFunc

This Function is running and does stuff to the gui, my question is how can i stop it when the user presses the stop button ?

i tried the on event button to run something like $Exit = 1 but $Exit wont update till the running func will end

Edited by DaProgrammer

Share this post


Link to post
Share on other sites
Generator

AdilbEnable()

Share this post


Link to post
Share on other sites
DaProgrammer

AdilbEnable()

i though there might be a better way :)

ill try implementing it and hopefully it will work or some1 will suggest something better.


Share this post


Link to post
Share on other sites
DaProgrammer

post a snippet that has the Stop button to show what you are doing ...

$Terminate = GUICtrlCreateButton("Exit",400,$WorkSpaceH+45,80,40)
 GUICtrlSetOnEvent($Terminate,"Terminate")
 
 Func Terminate()
     Exit
 EndFunc

i tried to exit instead of stopping but it only exits when the loop is done


Share this post


Link to post
Share on other sites
DaProgrammer

Adlib wont work since i cant read the button being pressed untill the func ends, so correct me if im wrong but its the same as a regular button

EDIT : is there a way to check if the button was pressed in the last couple of seconds ? or leave the button in pressed position untill its read and raised ?

Edited by DaProgrammer

Share this post


Link to post
Share on other sites
SmOke_N

Adlib wont work since i cant read the button being pressed untill the func ends, so correct me if im wrong but its the same as a regular button

You're better off just using guigetmsg and doing away with onevent... put the guigetmsg in whatever loop you are currently in.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
DaProgrammer

You're better off just using guigetmsg and doing away with onevent... put the guigetmsg in whatever loop you are currently in.

can i use guigetmsg when in on event mode ? couse the prog does a lot of other stuff and im more comftable with on event

EDIT : From the help File

If the GUIOnEventMode option is set to 1 then the return from GUIGetMsg is always 0 and the @error is set to 1.
Edited by DaProgrammer

Share this post


Link to post
Share on other sites
DaProgrammer

Any more ideas ? there must be a way to terminate a loop with a button.

BTW : the loop doesnt have to be a "FOR" it can be any loop but i hardly think it will make a diffrence.


Share this post


Link to post
Share on other sites
Jos

$Terminate = GUICtrlCreateButton("Exit",400,$WorkSpaceH+45,80,40)
 GUICtrlSetOnEvent($Terminate,"Terminate")
 
 Func Terminate()
     Exit
 EndFunc

i tried to exit instead of stopping but it only exits when the loop is done

I mean a snippet we can run and modify ... I am lazy and don't want to have to code before being able to test. :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
DaProgrammer

I mean a snippet we can run and modify ... I am lazy and don't want to have to code before being able to test. :)

Opt("GUIOnEventMode",1)
GuiCreate("MyGUI", 392, 323,-1, -1)

$Button_1 = GuiCtrlCreateButton("Run", 210, 190, 100, 60)
GUICtrlSetOnEvent($Button_1,"Count")

$Button_2 = GuiCtrlCreateButton("Terminate", 50, 200, 110, 60)
GUICtrlSetOnEvent($Button_2,"Terminate")

$Label_3 = GuiCtrlCreateLabel("Label3", 130, 50, 100, 50)

GuiSetState()

While 1
Sleep(1000)
WEnd

Func Count()
    For $i = 1 to 10
        GUICtrlSetData($Label_3,$i)
        Sleep(1000)
        Next
EndFunc
    
Func Terminate()
    Exit
EndFunc

if u press run it will count to 10 and if u press Terminate when it reaches 3 (just an example) it will w8 till 10 and then terminate


Share this post


Link to post
Share on other sites
Jos

Opt("GUIOnEventMode",1)
GuiCreate("MyGUI", 392, 323,-1, -1)

$Button_1 = GuiCtrlCreateButton("Run", 210, 190, 100, 60)
GUICtrlSetOnEvent($Button_1,"Count")

$Button_2 = GuiCtrlCreateButton("Terminate", 50, 200, 110, 60)
GUICtrlSetOnEvent($Button_2,"Terminate")

$Label_3 = GuiCtrlCreateLabel("Label3", 130, 50, 100, 50)

GuiSetState()

While 1
     Sleep(1000)
WEnd

Func Count()
    For $i = 1 to 10
        GUICtrlSetData($Label_3,$i)
        Sleep(1000)
        Next
EndFunc
    
Func Terminate()
    Exit
EndFunc

if u press run it will count to 10 and if u press Terminate when it reaches 3 (just an example) it will w8 till 10 and then terminate

Ok, see what you are doing now. Only one event is processed at a time so you need to return from an event Func as soon as possible to make the GUI respond again.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
DaProgrammer

Ok, see what you are doing now. Only one event is processed at a time so you need to return from an event Func as soon as possible to make the GUI respond again.

i know that exiting the function is the best way to check wheater to stop it or not.

STROKE OF GENIUS : what if i enable adlib at 1000ms to change count, and button terminate will stop the adlib :) going to test it now.

EDIT : it kinda works going to refine it and tell u how it came out ;)

Edited by DaProgrammer

Share this post


Link to post
Share on other sites
DaProgrammer

i used a Function that makes one step and the Run button starts that function as and adlib, and the terminate button stops that adlib.

So heres the solution for those intersted.


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  

×