Jump to content

Stoping a while loop with button in a GUI


Go to solution Solved by November,

Recommended Posts

Hi :)

I'm relatively new to Autoit and i've been trying to develop a simple countdown timer that starts at 3:30 and when it reaches 0 it alerts the user that time is up. This i've already done, the problem now is how to reset the countdown before it reaches 0 if the user chooses to.

For example, in my code below(again not a great master of autoit so feel free to give tips/opinions  :thumbsup: ) i have a simple GUI with a start and a stop button. The start button works fine and the countdown start but I can't stop it or reset it with the stop button

Is there a way to do this? can i use the stop button to break the while procedure running?

Or is there a better way to make a countdown timer?

 

Sorry for the bad english

Cheers

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

Global $OnTop

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

Func _Quit()
    Exit
EndFunc

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Timer", 143, 140, 1238, 136)
$Input = GUICtrlCreateInput("", 35, 10, 77, 30, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
GUICtrlSetFont($Input, 18, 800, 2, "Calibri")
GUICtrlSetColor($Input, 0x008080)
$Inicio = GUICtrlCreateButton("Iniciar", 35, 43, 78, 40, 0)
$Fim = GUICtrlCreateButton("Limpar", 35, 83, 78, 40, 0)
GUICtrlSetState($Fim, $GUI_DISABLE)
GUISetState(@SW_SHOW)
WinSetOnTop($Form1,"",1)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        
     Case $GUI_EVENT_CLOSE
            Exit
 
   Case $Inicio 

   $ms = 211000 ; 3:30 em Milisegundos
   $timer=TimerInit()   
   GUICtrlSetState($Fim, $GUI_ENABLE)
   GUICtrlSetState($Inicio, $GUI_DISABLE)
  
   While TimerDiff($timer) < $ms 
   $seconds = TimerDiff($timer)/1000
   $diff = $seconds - ($ms/1000);
   $minutes = Int($diff / 60)
   $secondsRem = $diff - ($minutes * 60)
   $minutes = $minutes * -1
   $secondsRem = $secondsRem * -1
   $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
   GUICtrlSetData($Input, $time)
   WEnd
  
   Beep(1500, 45)
   Sleep(50)
   Beep(1500, 45)
   Do
   $msg = MsgBox(0, "ALERTA!!" ,"VOLTAR Á LINHA!!!", 1)
   Sleep(50)
   Until $msg = 1  
   
   Case $Fim

       MsgBox(0,"TESTE","CENAS")
       ExitLoop
    
    EndSwitch
  
WEnd
Link to comment
Share on other sites

Hi,

The problem of your script is that while the timer is counting down you are in a subloop without a GUIGetMsg to retreive the user actions.

You have some options, but the more simple for you here would be to replace the subloop with an "If".

Br, FireFox.

Link to comment
Share on other sites

Hi FireFox and thank you for your reply

As i'm still a newbie in this can you exemplify in code pls?

I've tryed to replace the While by an if but it just skyped to the final message without making the coutdown

Thanks in advance

Link to comment
Share on other sites

oh right I didn't read the entire code.

First of all, move out the countdown and the message out of the switch statement.

Put this into an if with the condition of $timer <> 0 (so initialize the var to 0 at the top of your script) and when the timer exceeds the limit put it back to 0 (just after or before the message; so it won't match the "If" again).

And inside your button just let the timerinit with your guictrlsetstate.

Try it, and if you're running into problems post what you've done.

Also I advice you to use the Tidy tool (Ctrl + T), it will beautiful your code ;)

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Hi there,

I did a workaround without messing all the code.

Global $OnTop

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

Func _Quit()
    Exit
EndFunc

;_IsPressed

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Timer", 143, 140, 1238, 136)
$Input = GUICtrlCreateInput("", 35, 10, 77, 30, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
GUICtrlSetFont($Input, 18, 800, 2, "Calibri")
GUICtrlSetColor($Input, 0x008080)
$Inicio = GUICtrlCreateButton("Iniciar", 35, 43, 78, 40, 0)
$Fim = GUICtrlCreateButton("Limpar", 35, 83, 78, 40, 0)
GUICtrlSetState($Fim, $GUI_DISABLE)
GUISetState(@SW_SHOW)
WinSetOnTop($Form1,"",1)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

     Case $GUI_EVENT_CLOSE
        Exit

    Case $Inicio
       $Stop = GUICtrlCreateButton("Stop", 35, 43, 78, 40, 0)
       $ms = 211000 ; 3:30 em Milisegundos
       $timer=TimerInit()
       GUICtrlSetState($Fim, $GUI_ENABLE)
       GUICtrlSetState($Inicio, $GUI_DISABLE)

       While TimerDiff($timer) < $ms
           $nMsg = GUIGetMsg()
           if  $nMsg = $Stop Then
                ExitLoop
            Else
           $seconds = TimerDiff($timer)/1000
           $diff = $seconds - ($ms/1000);
           $minutes = Int($diff / 60)
           $secondsRem = $diff - ($minutes * 60)
           $minutes = $minutes * -1
           $secondsRem = $secondsRem * -1
           if $nMsg = $Stop Then ExitLoop
           $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
           GUICtrlSetData($Input, $time)
           ;if $nMsg = $Stop Then ExitLoop
       EndIf
        WEnd

   Beep(1500, 45)
   Sleep(50)
   Beep(1500, 45)
   Do
   $msg = MsgBox(0, "ALERTA!!" ,"VOLTAR Á LINHA!!!", 1)
   Sleep(50)
   Until $msg = 1

    EndSwitch

WEnd

func fim()
    MsgBox(0,"TESTE","CENAS")
    Exit
EndFunc

I hope this helps.

P.S. - És Português?  :bye: 

Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Link to comment
Share on other sites

FireFox and November thanks for both your replies

Excelent, I didn't think to use the if inside the while to make it stop

November i've used your example and made some modifications but there is still one problem i'm facing

The stop button only works once

After that it just goes back to the way it was with the while ignoring every action the user takes

Here's the code with the modifications

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

Global $OnTop

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

Func _Quit()
    Exit
EndFunc


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Timer", 143, 140, 1238, 136)
$Input = GUICtrlCreateInput("", 35, 10, 77, 30, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
GUICtrlSetFont($Input, 18, 800, 2, "Calibri")
GUICtrlSetColor($Input, 0x008080)
$Inicio = GUICtrlCreateButton("Iniciar", 35, 43, 78, 40, 0)
GUISetState(@SW_SHOW)
WinSetOnTop($Form1,"",1)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

     Case $GUI_EVENT_CLOSE
        Exit

    Case $Inicio
       $Stop = GUICtrlCreateButton("Stop", 35, 83, 78, 40, 0)
       $ms = 211000 ; 3:30 em Milisegundos
       $timer=TimerInit()

       While TimerDiff($timer) < $ms
           $nMsg = GUIGetMsg()
           if  $nMsg = $Stop Then
                ExitLoop
            Else
           $seconds = TimerDiff($timer)/1000
           $diff = $seconds - ($ms/1000);
           $minutes = Int($diff / 60)
           $secondsRem = $diff - ($minutes * 60)
           $minutes = $minutes * -1
           $secondsRem = $secondsRem * -1
           if $nMsg = $Stop Then ExitLoop
           $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
           GUICtrlSetData($Input, $time)
          
       EndIf
   WEnd
   
    fim()
  
    EndSwitch

WEnd

func fim()
        
If $Time = "00:00" then 
   Beep(1500, 45)
   Sleep(50)
   Beep(1500, 45)
   Do
   $msg = MsgBox(0, "ALERTA!!" ,"VOLTAR Á LINHA!!!", 1)
   Sleep(50)
   Until $msg = 1
   
   
  Endif 
EndFunc

P.S. - Yap :bye: obrigado pela a ajuda tou a começar a entrar no autoit ainda estou muito verde lol 

 

Link to comment
Share on other sites

  • Solution

FireFox and November thanks for both your replies

Excelent, I didn't think to use the if inside the while to make it stop

November i've used your example and made some modifications but there is still one problem i'm facing

The stop button only works once

After that it just goes back to the way it was with the while ignoring every action the user takes

Here's the code with the modifications

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

Global $OnTop

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

Func _Quit()
    Exit
EndFunc


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Timer", 143, 140, 1238, 136)
$Input = GUICtrlCreateInput("", 35, 10, 77, 30, BitOr($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
GUICtrlSetFont($Input, 18, 800, 2, "Calibri")
GUICtrlSetColor($Input, 0x008080)
$Inicio = GUICtrlCreateButton("Iniciar", 35, 43, 78, 40, 0)
GUISetState(@SW_SHOW)
WinSetOnTop($Form1,"",1)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

     Case $GUI_EVENT_CLOSE
        Exit

    Case $Inicio
       $Stop = GUICtrlCreateButton("Stop", 35, 83, 78, 40, 0)
       $ms = 211000 ; 3:30 em Milisegundos
       $timer=TimerInit()

       While TimerDiff($timer) < $ms
           $nMsg = GUIGetMsg()
           if  $nMsg = $Stop Then
                ExitLoop
            Else
           $seconds = TimerDiff($timer)/1000
           $diff = $seconds - ($ms/1000);
           $minutes = Int($diff / 60)
           $secondsRem = $diff - ($minutes * 60)
           $minutes = $minutes * -1
           $secondsRem = $secondsRem * -1
           if $nMsg = $Stop Then ExitLoop
           $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
           GUICtrlSetData($Input, $time)
          
       EndIf
   WEnd
   
    fim()
  
    EndSwitch

WEnd

func fim()
        
If $Time = "00:00" then 
   Beep(1500, 45)
   Sleep(50)
   Beep(1500, 45)
   Do
   $msg = MsgBox(0, "ALERTA!!" ,"VOLTAR Á LINHA!!!", 1)
   Sleep(50)
   Until $msg = 1
   
   
  Endif 
EndFunc

P.S. - Yap :bye: obrigado pela a ajuda tou a começar a entrar no autoit ainda estou muito verde lol 

 

 

Yeap just create another button in main window and will be ok, that was a sugestion that was as bringing to you!

Keep being curious and script a lot! LOL

P.S. - Se precisares de alguma coisa manda-me PM, tenho alguma experiência mas não muita.

Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

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