Jump to content

Exiting loop via GUI stop button


Gulae
 Share

Recommended Posts

Hi all,

hopefully someone can help me out on this one. I only discovered autoit 1 month ago, and I'm starting with the GUI part now ;-)

At the moment I have a script that collects some data from a website that runs as long as I don't stop it manually. Now I want to build a small GUI around my script, but I came across the following problem :

I have foreseen 2 buttons in my GUI -> START STOP.

When I push the START button, I will call a function that is my current script.

The problem is that this function will run forever and will never give back the control to my GUI,

so I will never be able to push that STOP button.

Is there any way around this?

CODE
#include <GuiConstants.au3>

GuiCreate("STOP/START Script", 200, 150)

$startbutton = GUICtrlCreateButton("Start !", 50, 50, 100, 30)

$stopbutton = GuiCtrlCreateButton("Stop !", 50, 90, 100, 30)

GUISetState()

While 1

$msg = GUIGetMsg()

Switch $msg

Case $startbutton

StartButton()

Case $GUI_EVENT_CLOSE

Exit

EndSwitch

Wend

Func StartButton()

While 1

WEnd

EndFunc

Link to comment
Share on other sites

Think you'll need to use On Event instead of GuiGetMsg

Just tried doing the same with an OnEvent, but the result stays the same. The function doesn't exit the inf. loop to give the control back to the main process. (see example)

CODE
#include <GuiConstants.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

$mainwindow = GuiCreate("STOP/START Script", 200, 150)

$startbutton = GUICtrlCreateButton("Start !", 50, 50, 100, 30)

$stopbutton = GuiCtrlCreateButton("Stop !", 50, 90, 100, 30)

GUISetOnEvent($GUI_EVENT_CLOSE, "StopButton")

GUICtrlSetOnEvent($startbutton, "StartButton")

GUICtrlSetOnEvent($stopbutton, "StopButton")

GUISetState(@SW_SHOW)

While 1

Sleep(1000)

Wend

Func StartButton()

MsgBox(0, "GUI Event", "starting infinitive loop")

While 1

WEnd

EndFunc

Func StopButton()

MsgBox(0, "GUI Event", "stopping pgm")

Exit

EndFunc

I think I need to find a solution where the function returns back to the main process after a single execution. At this point, the main process can check if an event was triggerd, if not, the function can be executed again ...

Link to comment
Share on other sites

You can always use HotKeySet to exit the program, I have tried and it works.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Thx Bigdod,

this works indeed (allthough it is not as beautifull as when you can click a button via an interface ;-) )

#include <GuiConstants.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

$started = 0
$mainwindow = GuiCreate("STOP/START Script", 200, 190)
$startbutton = GUICtrlCreateButton("Start !", 50, 50, 100, 30)
$stopbutton = GuiCtrlCreateButton("Stop !", 50, 90, 100, 30)
$btnExit = GUICtrlCreateButton("Exit", 50, 130, 100, 30)

GUISetOnEvent($GUI_EVENT_CLOSE, "StopButton")
GUICtrlSetOnEvent($startbutton, "StartButton")
GUICtrlSetOnEvent($stopbutton, "StopButton")
GUICtrlSetOnEvent($btnExit, "_Terminate")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
    If $started Then
        ConsoleWrite("this will fire until stop button is pressed" & @LF)
    EndIf
Wend

Func _Terminate()
    Exit
EndFunc

Func StartButton()
    MsgBox(0, "GUI Event", "starting infinitive loop")
    $started = 1
EndFunc

Func StopButton()
    MsgBox(0, "GUI Event", "stopping pgm")
    $started = 0
;~  Exit
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Wow Gafrost,

You're the man :) I knew it had to be something like your code, just overlooked to put my code in the already present while loop.

I just know I'm gonna love this scripting language and this forum with all you experts,

thx a lot for the help

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