Jump to content

Loop causing non-working controls in Application


 Share

Recommended Posts

AutoIT Versions: 3.3.14.5 & 3.3.14.2

Once the Counter Button is pressed all events stop working.

I understand the issue is the loop inside displayLoop Function prevents the inital event from stopping and new events from occuring.
Not sure how to make this application work without the loop inside displayLoop function.

Originally I used images to display the counter numbers but since you do not have access to that I changed the code to display text numbers.

Trying to create a Timer Counter eventually for time management.  How do I make the counter count without the loop or does it need to be somewhere else?

In another thread Jos said to look at this for help --> https://www.autoitscript.com/wiki/Managing_Multiple_GUIs

Though in my use case I do not understand.


  My Code Below
---------------------------

 

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <FontConstants.au3>

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

HotKeySet("{ESC}", "endApp")

Global $title = "A Little Counter Application"
Global $stopLoop = "No"
Global $imageNumbers[11]  = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":"]

Global $theApplication = GUICreate($title, 300, 200, -1,  -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "endApp")

Global $btnStartDisplayLoop = GUICtrlCreateButton("Start Count", 90, 120, 100, 30)
GUICtrlSetOnEvent($btnStartDisplayLoop, "btnPress")

GUISetFont(24,  $FW_NORMAL, $GUI_FONTNORMAL, "Arial")
Global $imgMIN1 = GUICtrlCreateLabel($imageNumbers[0], 50, 50)
Global $imgMIN2 = GUICtrlCreateLabel($imageNumbers[0], 100, 50)
Global $spacer  = GUICtrlCreateLabel($imageNumbers[10], 150, 50)
Global $imgSEC1 = GUICtrlCreateLabel($imageNumbers[0], 200, 50)
Global $imgSEC2 = GUICtrlCreateLabel($imageNumbers[0], 250, 50)

GUISetState(@SW_SHOW, $theApplication)

Func displayLoop($duration = 5)
    Dim $stopLoop, $min, $sec

    While 1
        if $stopLoop == "Yes" Then ExitLoop

        If $min == $duration Then
            ExitLoop
        EndIf

        $sec = $sec + 1
        If($sec == 60) Then
            $min = $min + 1
            $sec = 0
        EndIf

        $mins = returnArray($min)

        GUICtrlSetData($imgMIN1, $imageNumbers[$mins[1]])
        GUICtrlSetData($imgMIN2, $imageNumbers[$mins[2]])

        $secs = returnArray($sec)
        GUICtrlSetData($imgSEC1, $imageNumbers[$secs[1]])
        GUICtrlSetData($imgSEC2, $imageNumbers[$secs[2]])

        Sleep(1000)
    WEnd

    MsgBox(0,"","Counter Stopped")

EndFunc

Func returnArray($number)
    Local $newArray[3]
    if $number = "" Then $number = 0
    Local $numberArray = StringSplit($number, "")
    If($numberArray[0] == 1) Then
        $newArray[0] = 2
        $newArray[1] = 0
        $newArray[2] = $numberArray[1]
        $numberArray = $newArray
    EndIf
    return $numberArray
EndFunc

Func btnPress()
    Dim $stopLoop
    $getBtnText = GUICtrlRead($btnStartDisplayLoop, $GUI_READ_EXTENDED)
    If $getBtnText == "Start Count" Then
        GUICtrlSetData($btnStartDisplayLoop, "Stop Count")
        $stopLoop = "No"
        displayLoop() ; Starts Display Loop

    ElseIf $getBtnText == "Stop Count" Then
        GUICtrlSetData($btnStartDisplayLoop, "Start Count")
        $stopLoop = "Yes"
    EndIf
EndFunc

Func endApp()
    Local $exitCode = MsgBox(68,'',"Are you sure you want to quit?")
    If($exitCode == 6) Then
        GUIDelete($theApplication)
        Exit
    EndIf
EndFunc

While 1
;~ Keep Application Running
WEnd


Once button is initially pressed it does not respond and the close modal button does not work.  The hotkey {ESC} does work.

I am lost at how to make this work, please help.


 

Edited by iAmNewbe
Link to comment
Share on other sites

  • iAmNewbe changed the title to Loop causing non-working controls in Application

The issue is that once the Loop inside displayLoop starts all events, buttons stop working.

ContinueLoop would only come into play when their is a condition that when met the rest of the code is needed to be bypassed for that iteration of the loop.

In my use case and way the application is currently written, this is not something that fits with the existing code.

Link to comment
Share on other sites

  • Moderators

iAmNewbe,

The Wiki tutorial you need to read is Interrupting a running function - that explains how to do what you wish. I am a bit busy at the moment but if I find time I will try and modify your code this afternoon.

M23

Edit: Easier than I thought:

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <FontConstants.au3>

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

HotKeySet("{ESC}", "endApp")

Global $title = "A Little Counter Application"
Global $stopLoop = "Yes"
Global $imageNumbers[11]  = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":"]

Global $theApplication = GUICreate($title, 300, 200, -1,  -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "endApp")

Global $btnStartDisplayLoop = GUICtrlCreateButton("Start Count", 90, 120, 100, 30)
GUICtrlSetOnEvent($btnStartDisplayLoop, "btnPress")

GUISetFont(24,  $FW_NORMAL, $GUI_FONTNORMAL, "Arial")
Global $imgMIN1 = GUICtrlCreateLabel($imageNumbers[0], 50, 50)
Global $imgMIN2 = GUICtrlCreateLabel($imageNumbers[0], 100, 50)
Global $spacer  = GUICtrlCreateLabel($imageNumbers[10], 150, 50)
Global $imgSEC1 = GUICtrlCreateLabel($imageNumbers[0], 200, 50)
Global $imgSEC2 = GUICtrlCreateLabel($imageNumbers[0], 250, 50)

GUISetState(@SW_SHOW, $theApplication)

Func displayLoop($duration = 5)
    Dim $stopLoop, $min, $sec

    While 1
        if $stopLoop == "Yes" Then ExitLoop

        If $min == $duration Then
            ExitLoop
        EndIf

        $sec = $sec + 1
        If($sec == 60) Then
            $min = $min + 1
            $sec = 0
        EndIf

        $mins = returnArray($min)

        GUICtrlSetData($imgMIN1, $imageNumbers[$mins[1]])
        GUICtrlSetData($imgMIN2, $imageNumbers[$mins[2]])

        $secs = returnArray($sec)
        GUICtrlSetData($imgSEC1, $imageNumbers[$secs[1]])
        GUICtrlSetData($imgSEC2, $imageNumbers[$secs[2]])

        Sleep(1000)
    WEnd

    MsgBox(0,"","Counter Stopped")

EndFunc

Func returnArray($number)
    Local $newArray[3]
    if $number = "" Then $number = 0
    Local $numberArray = StringSplit($number, "")
    If($numberArray[0] == 1) Then
        $newArray[0] = 2
        $newArray[1] = 0
        $newArray[2] = $numberArray[1]
        $numberArray = $newArray
    EndIf
    return $numberArray
EndFunc

Func btnPress()
    Dim $stopLoop
    $getBtnText = GUICtrlRead($btnStartDisplayLoop, $GUI_READ_EXTENDED)
    If $getBtnText == "Start Count" Then
        GUICtrlSetData($btnStartDisplayLoop, "Stop Count")
        $stopLoop = "No"
        $vRun = True
        ;displayLoop() ; Starts Display Loop

    ElseIf $getBtnText == "Stop Count" Then
        GUICtrlSetData($btnStartDisplayLoop, "Start Count")
        $stopLoop = "Yes"
    EndIf
EndFunc

Func endApp()
    Local $exitCode = MsgBox(68,'',"Are you sure you want to quit?")
    If($exitCode == 6) Then
        GUIDelete($theApplication)
        Exit
    EndIf
EndFunc

While 1
    ;~ Keep Application Running
    Sleep(10) ; Stop CPU from frying!!!!!!!

    If $stopLoop = "No" Then
        displayLoop() ; Starts Display Loop
    EndIf

WEnd

The tutorial explains why it works - but do ask if anything is still unclear.

Edited by Melba23

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

So the solution is to not start the second loop via the event of the button press and instead set flags?

I think I ran into this before in a different scenerio..  Weird.


I do have a question about the use of 

 

Quote

If $stopLoop = "No" Then


Shouldn't this be a comparison operator instead of an assignment? 

I see that it works the same both ways, when changing the code but I don't understand this.  Have seen it with loop examples in the documentation also, I thought that this would create never ending loops forever assigning to the variable being checked?
 

Link to comment
Share on other sites

  • Moderators

iAmNewbie,

Quote

So the solution is to not start the second loop via the event of the button press and instead set flags?

Did you read the tutorial to which I linked? If you start a function from the main loop it is interruptable by OnEvent calls - if you start it from an OnEvent call it is not.

Quote

Shouldn't this be a comparison operator instead of an assignment? 

If you read the Operators page in the Help file you will see that AutoIt is clever enough to use "=" as both - and that the "==" operator is a special case-sensitive comparison operator.

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

Yes, I understand.  I changed my code to reflect this and it works, thank you.

I had forgotten about that operator thing with AutoIt.  I do not use it an a daily basis and every other language I use does not have that. Thanks for reminding me.

I appreciate your help, you helped solve my problem.

Edited by iAmNewbe
Link to comment
Share on other sites

  • Moderators

iAmNewbie,

Glad I could help.

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

Doesn't it update once you release?

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

The problem is that it stops while the mouse button, primary, is held down.  I do not want it to stop at all.
If you add a time begin, when the count was started and a time stop when the count terminated the time does not match the counter.
This is an example of a use case where you do not want the script to pause.

Yes, once the button is released the count continues.  It should not stop at all though.

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