Sign in to follow this  
Followers 0
Floppy

Problem with 'Do' loop

25 posts in this topic

#1 ·  Posted (edited)

Hi, I have this script:

#include <GuiConstants.au3>

GUICreate("test")
$label2=GUICtrlCreateLabel("",20,20,100,20)
GUISetState()

$m=GUIGetMsg()
Do
Sleep(1000)
GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
Until $m=$GUI_EVENT_CLOSE

If $m=$GUI_EVENT_CLOSE Then Exit

Why if I click Close button, doesn't the clock stop and doesn't the window close?

bye

Edited by FSoft

Share this post


Link to post
Share on other sites



maybe because the line

$m=GUIGetMsg()

isn't inside the do-loop...

A-Jay


Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Share this post


Link to post
Share on other sites

but i think you should look for 'GUIOnEventMode' help

Opt("GUIOnEventMode", 1) ; We want the program working on events

option.


Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Share this post


Link to post
Share on other sites

I tried both solutions...but it does NOT works!

Share this post


Link to post
Share on other sites

while & Wend?....

Share this post


Link to post
Share on other sites

#include <GuiConstants.au3>

GUICreate("test")
$label2=GUICtrlCreateLabel("",20,20,100,20)
GUISetState()

$x=0
while 1
    Sleep(5)
    $x = $x+1
    $m=GUIGetMsg()
    if $x = 5 then
        GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
        $x=0
    EndIf
    If $m=$GUI_EVENT_CLOSE Then Exit
wend

Share this post


Link to post
Share on other sites

FSoft,

It does not work for 2 reasons:

1. You never poll GUIGetMsg in the loop, so how is AutoIt supposed to know you have clicked on anything?

2. The vast majority of your time in the loop is spent in the Sleep(1000) when the script is unresponsive. So even if you were to poll GUIGetMsg you woudl be very unlikely to get a response.

This will do what you want:

#include <GuiConstantsEx.au3>

GUICreate("test")
$label2=GUICtrlCreateLabel("",20,20,100,20)
GUISetState()

Do
    GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
    $begin = TimerInit()
    While TimerDiff($begin) < 1000
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
    WEnd
Until 1 = 2

But there is a better way get your label to update without a flicker - look here.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

correct me if im wrong but uhh... while its in sleep doesnt it que the msg untill its done the sleep?

Share this post


Link to post
Share on other sites

CodyBarrett,

I did try before posting. ;-)

The length of the Sleep seems critical. Have a play with this simple code with the Sleep set at various values:

GUICreate("Test", 200, 200)
$hBut = GUICtrlCreateButton("Test", 10, 10, 80, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hBut
            ConsoleWrite("Pressed" & @CRLF)
    EndSwitch
    Sleep(1000)
WEnd

For me it works fine with Sleep(100). At 500 it appears to stack some button presses and reject others. At 1000 it is pretty much unresponsive. So I can only suppose that Sleep means exactly that - minus the snoring!

Perhaps one of the gurus can enlighten us further?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

FSoft,

It does not work for 2 reasons:

1. You never poll GUIGetMsg in the loop, so how is AutoIt supposed to know you have clicked on anything?

2. The vast majority of your time in the loop is spent in the Sleep(1000) when the script is unresponsive. So even if you were to poll GUIGetMsg you woudl be very unlikely to get a response.

This will do what you want:

#include <GuiConstantsEx.au3>

GUICreate("test")
$label2=GUICtrlCreateLabel("",20,20,100,20)
GUISetState()

Do
    GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
    $begin = TimerInit()
    While TimerDiff($begin) < 1000
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
    WEnd
Until 1 = 2

But there is a better way get your label to update without a flicker - look here.

M23

I have one button: I want to press it once to start the clock and twice to stop it.

I tried this, but the clock doesn't start:

While Not GUIGetMsg()=$go
If @SEC<>$curr_sec Then
GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
$curr_sec=@SEC
EndIf
If GUIGetMsg()=$GUI_EVENT_CLOSE Then Exit
WEnd

Any solution?

Share this post


Link to post
Share on other sites

FSoft,

Look at this:

#include <GuiConstantsEx.au3>

GUICreate("test", 200, 200)
$label2=GUICtrlCreateLabel("",20,20,100,20)
$button = GUICtrlCreateButton("Start", 20, 100, 80, 30)
GUISetState()

$fTimer = False

While 1
    If $fTimer Then
        GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
        $begin = TimerInit()
        While TimerDiff($begin) < 1000
            Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $button
                    $fTimer = Not $fTimer
                    GUICtrlSetData($button, "Start")
                    GUICtrlSetData($label2, "")
            EndSwitch
        WEnd
    Else
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $button
                $fTimer = Not $fTimer
                GUICtrlSetData($button, "Stop")
        EndSwitch
    EndIf
WEnd

You have been around here for a while - you should be able to solve these simple problems on your own by now. :-)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

What is the variable $fTimer for?

------------

Sometimes, if I click quickly, the clock doesn't start...why?

Edited by FSoft

Share this post


Link to post
Share on other sites

If you are calling GUIGetMsg you don't even need Sleep. GUIGetMsg in AutoIt will use a small sleep every time.

Share this post


Link to post
Share on other sites

FSoft,

$fTimer is a boolean (True/False) variable which is set according to the state of the timer: True means timer running/label visible - False means timer off/label invisible. This flag variable then determines which While...WEnd loop to use and so what happens when the button is clicked. You need this kind of logic because you said you only wanted one button - if you use two buttons, then each button can have its own series of commands. As an aside, one of the nice things about boolean variables is that you can change their state by using Not as you can see in the script.

Here is another way of coding the same thing. In this script the label exists all the time, but you only see it when you start the timer:

#include <GuiConstantsEx.au3>

GUICreate("Timer Test", 200, 200)
$label2 = GUICtrlCreateLabel("",20,20,100,20)
GUICtrlSetState(-1, $GUI_HIDE)
$button = GUICtrlCreateButton("Start", 20, 100, 80, 30)
GUISetState()

$fTimer = False

$begin = TimerInit()

While 1
    
    If TimerDiff($begin) > 1000 Then
        GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
        $begin = TimerInit()
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            $fTimer = Not $fTimer
            If $fTimer Then
                GUICtrlSetData($button, "Stop")
                GUICtrlSetState($label2, $GUI_SHOW)
            Else
                GUICtrlSetData($button, "Start")
                GUICtrlSetState($label2, $GUI_HIDE)
            EndIf
    EndSwitch
        
WEnd

I must say I prefer this script to the earlier one.

As to why the timer does not start if you click too quickly - do you mean nothing happens at all? If so, then AutoIt/Windows has not recognised the click as valid.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

FSoft,

$fTimer is a boolean (True/False) variable which is set according to the state of the timer: True means timer running/label visible - False means timer off/label invisible. This flag variable then determines which While...WEnd loop to use and so what happens when the button is clicked. You need this kind of logic because you said you only wanted one button - if you use two buttons, then each button can have its own series of commands. As an aside, one of the nice things about boolean variables is that you can change their state by using Not as you can see in the script.

Here is another way of coding the same thing. In this script the label exists all the time, but you only see it when you start the timer:

#include <GuiConstantsEx.au3>

GUICreate("Timer Test", 200, 200)
$label2 = GUICtrlCreateLabel("",20,20,100,20)
GUICtrlSetState(-1, $GUI_HIDE)
$button = GUICtrlCreateButton("Start", 20, 100, 80, 30)
GUISetState()

$fTimer = False

$begin = TimerInit()

While 1
    
    If TimerDiff($begin) > 1000 Then
        GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
        $begin = TimerInit()
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            $fTimer = Not $fTimer
            If $fTimer Then
                GUICtrlSetData($button, "Stop")
                GUICtrlSetState($label2, $GUI_SHOW)
            Else
                GUICtrlSetData($button, "Start")
                GUICtrlSetState($label2, $GUI_HIDE)
            EndIf
    EndSwitch
        
WEnd

I must say I prefer this script to the earlier one.

As to why the timer does not start if you click too quickly - do you mean nothing happens at all? If so, then AutoIt/Windows has not recognised the click as valid.

M23

Thanks for reply.

But the problem is the same.

If I click too quickly, nothing happens. Then, if I click again the clock hides. Why?

Share this post


Link to post
Share on other sites

FSoft,

If I click too quickly, nothing happens. Then, if I click again the clock hides.

That does not make sense.

Look at the script I posted last. The clock is hidden at start, so your click must work if the clock appears. If the clock appears, it runs - because it is running all the time. If you say the the clock disappears when you click a second time - it must logically have been running beforehand. So I do not see how your first click does nothing - and then the second click does. The second click cannot do anything until the first has shown the clock.

What does the button text say at each point? As you can see from the script, this changes to reflect what the button will do when next clicked - it says "Stop" when the clock is displayed and "Start" when it is hidden.

Anyway the solution is in your hands and is very simple - do not click quickly! :-)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

FSoft,

That does not make sense.

Look at the script I posted last. The clock is hidden at start, so your click must work if the clock appears. If the clock appears, it runs - because it is running all the time. If you say the the clock disappears when you click a second time - it must logically have been running beforehand. So I do not see how your first click does nothing - and then the second click does. The second click cannot do anything until the first has shown the clock.

What does the button text say at each point? As you can see from the script, this changes to reflect what the button will do when next clicked - it says "Stop" when the clock is displayed and "Start" when it is hidden.

Anyway the solution is in your hands and is very simple - do not click quickly! :-)

M23

On the GUI I created three inputbox, the first for hour, the second for minutes and the third for seconds.

How can I hide the clock when the hours, the minutes and the seconds match?

Share this post


Link to post
Share on other sites

FSoft,

Please post the code you are using now. It is very difficult to offer sensible advice if you are not using the code which was last posted!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

FSoft,

Please post the code you are using now. It is very difficult to offer sensible advice if you are not using the code which was last posted!

M23

I'm using the same code:

Here it is:

#include <GuiConstantsEx.au3>

GUICreate("Timer Test", 200, 200)
$label2 = GUICtrlCreateLabel("",20,20,100,20)
GUICtrlSetState(-1, $GUI_HIDE)
$button = GUICtrlCreateButton("Start", 20, 100, 80, 30)
$hour=GUICtrlCreateInput("",20,150,20,20)
$min=GUICtrlCreateInput("",50,150,20,20)
$sec=GUICtrlCreateInput("",80,150,20,20)
GUISetState()

$fTimer = False

$begin = TimerInit()

While 1
    
    If TimerDiff($begin) > 1000 Then
        GUICtrlSetData($label2,@HOUR&":"&@MIN&":"&@SEC)
        $begin = TimerInit()
    EndIf
    If $label2=GUICtrlRead($hour)&":"&GUICtrlRead($min)&":"&GUICtrlRead($sec) Then
        GUICtrlSetState($label2, $GUI_HIDE)
        EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            $fTimer = Not $fTimer
            If $fTimer Then
                GUICtrlSetData($button, "Stop")
                GUICtrlSetState($label2, $GUI_SHOW)
            Else
                GUICtrlSetData($button, "Start")
                GUICtrlSetState($label2, $GUI_HIDE)
            EndIf
    EndSwitch
    
WEnd

I don't know why it doesn't work!

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  
Followers 0