Jump to content

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


Recommended Posts

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

  • Developers

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

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

  • Moderators

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.

Link to comment
Share on other sites

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

  • Developers

$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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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