Jump to content

Infinite while loop and a stop button:)


 Share

Recommended Posts

Dear community,

I would like to make a program where a user can enter a word and clicks the start button, and the program will then repeat the entered word every few seconds until the user presses the stop button. I have made example programs to demonstrate my problem.

First version:

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author:      anarchypower
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>

GUICreate("Test-program", 230, 300)
GUICtrlCreateLabel("Please enter text below and press start:", 20, 10)
$input = GUICtrlCreateInput("Your text here", 20, 35)
$startbutton = GUICtrlCreateButton("Start", 30, 70, 60)
$stopbutton = GUICtrlCreateButton("Stop", 130, 70, 60)
$editor = GUICtrlCreateEdit("This edit box is for testing", 30, 120, 170, 150)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()

    Select
        Case $msg = $startbutton
            $text = GUICtrlRead($input)
            
            While 1     ;Program is stuck here, therefore the stop button
                Send($text);is not functional. At least thats what I think...
                Sleep(2000)
            WEnd
            
        Case $msg = $stopbutton
            ExitLoop
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
    EndSelect
WEnd

This is a small test program which contains an input box and an edit box to test the program's functionality. The program works as expected and repeats the word every 2 seconds, however this repeating can not be stopped. The stop button is not functional and the program is stuck in the while loop. This is pretty logic and to overcome this problem, I tried to redesign the code.

Second version: (a bit messy)

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author:      anarchypower
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>

GUICreate("Test-program", 230, 300)
GUICtrlCreateLabel("Please enter text below and press start:", 20, 10)
$input = GUICtrlCreateInput("Your text here", 20, 35)
$startbutton = GUICtrlCreateButton("Start", 30, 70, 60)
$stopbutton = GUICtrlCreateButton("Stop", 130, 70, 60)
$editor = GUICtrlCreateEdit("This edit box is for testing", 30, 120, 170, 150)
GUISetState(@SW_SHOW)
$go = 0

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $startbutton
            $go = 1
        Case $msg = $stopbutton
            $go = 0
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
    EndSelect

    If $go = 1 Then
        $text = GUICtrlRead($input)
        Send($text)
        Sleep(1000)
    Else
    EndIf

WEnd

By using a conditional statement, I was hoping to solve the problem of my stop button, but unfortunately, I was unable to solve it. I'm going to try and use the OnEventMode now, but Im afraid that will also not be able to exit my endless while loop.

There should be a way to give a stop button priority in my program? Whatever the program is doing, even if its running an infinite loop, there should be a way to stop the program (besides killing the process)?

I will update this post when I have finished my OnEventMode test program...

Greetings :)

Edited by anarchypower
Link to comment
Share on other sites

what's that else showing in the if $go statement ?!

anyway go take a look at the adlib enable/disable functions in the helpfile

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

Yeah the Else is not needed.

I will have a look at those adlib functions right away! :lmao:

Edit:

Using adlib:

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author:      anarchypower
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>

GUICreate("Test-program", 230, 300)
GUICtrlCreateLabel("Please enter text below and press start:", 20, 10)
$input = GUICtrlCreateInput("Your text here", 20, 35)
$startbutton = GUICtrlCreateButton("Start", 30, 70, 60)
$stopbutton = GUICtrlCreateButton("Stop", 130, 70, 60)
$editor = GUICtrlCreateEdit("This edit box is for testing", 30, 120, 170, 150)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $startbutton
            AdlibEnable("type_text", 100)
        Case $msg = $stopbutton
            AdlibDisable()
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
    EndSelect
WEnd

Func type_text()
        Sleep(1000)
        $text = GUICtrlRead($input)
        Send($text)
EndFunc

Perhaps I made a wrong construction, but it's not working properly :) The stop button is still not functioning.

I think I know why the stop button doesn't work. When the script is running my type_text() function, there is a sleep(1000) command. When the script is sleeping, the stop button press is ignored I think...I'm not sure. My stop button should be able to stop the script even when it's sleeping...

Edited by anarchypower
Link to comment
Share on other sites

I found something that works. By using hotkeys I am able to terminate the program while it is running an infinite loop.

Proof:

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author:      anarchypower
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>

GUICreate("Test-program", 230, 300)
GUICtrlCreateLabel("Please enter text below and press start:", 20, 10)
$input = GUICtrlCreateInput("Your text here", 20, 35)
$startbutton = GUICtrlCreateButton("OK", 30, 70, 60)
$stopbutton = GUICtrlCreateButton("Stop", 130, 70, 60)
_GUICtrlButton_SetImage($stopbutton, "delete.ico")
$editor = GUICtrlCreateEdit("Click here to test the program", 30, 120, 170, 150)

HotKeySet("^!x", "MyExit")

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()

    Select
        Case $msg = $startbutton
            $text = GUICtrlRead($input)
            While 1
                Send($text)
                Sleep(1000)
            WEnd
        Case $msg = $stopbutton
            ExitLoop
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
    EndSelect
WEnd

Func MyExit()
    Exit
EndFunc

The same this should be possible by using buttons?

Link to comment
Share on other sites

  • Moderators

anarchypower,

Have a look at this:

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author:      anarchypower
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>

GUICreate("Test-program", 230, 300)
GUICtrlCreateLabel("Please enter text below and press start:", 20, 10)
$input = GUICtrlCreateInput("Your text here", 20, 35)
$startbutton = GUICtrlCreateButton("Start", 30, 70, 60)
$stopbutton = GUICtrlCreateButton("Stop", 130, 70, 60)
$editor = GUICtrlCreateEdit("This edit box is for testing", 30, 120, 170, 150)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    
    Select
        Case $msg = $startbutton
            ExitLoop
        Case $msg = $stopbutton
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSelect
WEnd

$text = GUICtrlRead($input)
$Begin = TimerInit()

While 1
    $msg = GUIGetMsg()
    
    If $msg <> 0 Then ConsoleWrite($msg & @CRLF)

    Select
        Case $msg = $stopbutton
            Exit
            
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit        
    EndSelect
    
    If TimerDiff($Begin) > 2000 Then
        GUICtrlSetData($editor, @CRLF & $text, "-")
        $Begin = TimerInit()
    EndIf
WEnd

Exit

You need to put the "start" command outside the main loop. Ask if there is anything unclear (it works for me :-))

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

anarchypower,

TimerInit basically gives you the ms count at that moment, so the variable that you use is a record of that instant. Then TimerDiff gives you the ms elapsed since that moment.

Short example:

Time    Code                        Result

1000    $Begin = TimerInit()        $Begin = 1000
1001
1002
1003
1004
1005    $Since = TimerDiff($Begin)  $Since = (1005 - $Begin) -> 5ms
1006
1007
1008
1009
1010    $Since = TimerDiff($Begin)  $Since = (1010 - $Begin) -> 10ms

Clear?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 6 years later...

How about

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Authors:      anarchypower  
#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>


GUICreate("Test-program", 230, 300)
GUICtrlCreateLabel("Please enter text below and press start:", 20, 10)
$input = GUICtrlCreateInput("Your text here", 20, 35, 121, 21)
$startbutton = GUICtrlCreateButton("Start", 30, 70, 60, 25)
$stopbutton = GUICtrlCreateButton("Stop", 130, 70, 60, 25)
$Slider = GUICtrlCreateSlider(75, 110, 60, 29)
GUICtrlSetLimit(-1, 2, 1)
$Label1 = GUICtrlCreateLabel("Resume ",30, 114, 40,17)
$Label2 = GUICtrlCreateLabel("Pause", 144, 112, 43, 17)
$editor = GUICtrlCreateEdit("This edit box is for testing", 30,140, 170, 150)
GUISetState(@SW_SHOW)


Func TogglePause();===================== Pause ======================
    While  1
        ToolTip("Paused")
    $e = GUICtrlRead($Slider)
    If $e  = 1 Then
    ToolTip("")
    Return
    EndIf
    Sleep(100)
    WEnd
EndFunc   ;==>TogglePause

While 1
    $msg = GUIGetMsg()

    Select
        Case $msg = $startbutton
            ExitLoop
        Case $msg = $stopbutton
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSelect
WEnd

$text = GUICtrlRead($input)
$Begin = TimerInit()

While   1
    $msg = GUIGetMsg()
    $e = GUICtrlRead($Slider)
    If $e  = 2 Then
        TogglePause()
    EndIf

    If $msg <> 0 Then ConsoleWrite($msg & @CRLF)

    Select
        Case $msg = $stopbutton
            Exit

        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSelect

    If TimerDiff($Begin) > 2000 Then
        GUICtrlSetData($editor, @CRLF & $text, "-")
        $Begin = TimerInit()
    EndIf
WEnd

Exit
Link to comment
Share on other sites

This thread is 6 years old, I'd be willing to bet the OP has figured it out by now.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I am quite sure, there is a good chance, they did figured it out. Yet it never ceases to amaze me how many long-eared, slow, surefooted domesticated mammals there are on this forum that can not figure out how to be nice. But hey, there is still time.....


Elzie

Edited by Elzie
Link to comment
Share on other sites

I was being nice.

I kindly pointed out that the thread was 6 years old, and no where in my post did I flame you, insult you (as you have just done towards me), nor did I call you out for your violation of forum ethics. I just pointed out that the thread was old, and mentioned your post was probably unnecessary to the OP or anyone else after this length of time.

If you are so thin skinned as to feel slighted by something like that, that's all on you and not me. If you felt insulted by it, I apologize for making you feel that way as it was not my intention.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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