Jump to content

Timer will no longer count down.


Recommended Posts

I'm having an issue with my countdown timer. It will no longer countdown and it seems like it is blinking rapidly. I'm pretty sure it is due to the way I did $_HourCalc, since I couldn't find an example with that in it. I should also add that this is someone else's (niksigouin) script that I modified for my own use. I was wondering if anyone could help me out on the HourCalc or why it won't count down any longer. 

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


Global $_CompteArebour =0, $_Hours, $_Minutes, $_Seconds
$TimeTicks = TimerInit()

$Form1 = GUICreate("ImproTimer", 946, 535, -1, -1)
$TimerLabel = GUICtrlCreateLabel("", 352, 40, 247, 125, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetData($TimerLabel, StringFormat("%02u" & ":" & "%02u" & ":" & "%02u", $_Hours, $_Minutes, $_Seconds))
GUICtrlSetFont($TimerLabel, 72, 400, 0, "MS Sans Serif")
$BtnStart = GUICtrlCreateButton("Start", 352, 168, 121, 33)
$BtnPause = GUICtrlCreateButton("Pause", 352, 168, 121, 33)
GUICtrlSetState(-1, $GUI_HIDE)
$BtnReset = GUICtrlCreateButton("Reset", 480, 168, 121, 33)
$BtnAdd60 = GUICtrlCreateButton("60", 352, 208, 57, 33)
$BtnAdd30 = GUICtrlCreateButton("100", 416, 208, 57, 33)
$BtnAdd10 = GUICtrlCreateButton("120", 480, 208, 57, 33)
$BtnAdd5 = GUICtrlCreateButton("180", 544, 208, 57, 33)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $BtnStart                              ;<=== When pressed "Start", hide and then show "Pause"
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd60                              ;=== Add 1 minute to the Timer
            $_CompteArebour = $_CompteArebour + (60000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd30                              ;=== Add 30 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (100000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd10                              ;=== Add 10 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (120000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd5                               ;=== Add 5 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (180000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnReset                              ;=== Reset the Timer to 00:00
            $_CompteArebour = 0
            GUICtrlSetData($TimerLabel, StringFormat("%02u" & ":" & "%02u" & ":" & "%02u", 0, 0, 0))
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _Run()
    While 1
        _Check()
    WEnd
EndFunc   ;==>_Run

Func _Check() ;==> A function I took from an other example hoping to get an input to work
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $BtnStart                              ;<=== When pressed "Start", hide and then show "Pause"
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnPause                              ;<=== When pressed "Pause" hide and then show "Start"
            GUICtrlSetState($BtnPause, $GUI_HIDE)
            GUICtrlSetState($BtnStart, $GUI_SHOW)
        Case $BtnAdd60                              ;=== Add 1 minute to the Timer
            $_CompteArebour = $_CompteArebour + 3600000
        Case $BtnAdd30                              ;=== Add 30 seconds to the Timer
            $_CompteArebour = $_CompteArebour + 6000000
        Case $BtnAdd10                              ;=== Add 10 seconds to the Timer
            $_CompteArebour = $_CompteArebour + 7200000
        Case $BtnAdd5                               ;=== Add 5 seconds to the Timer
            $_CompteArebour = $_CompteArebour + 10800000
        Case $BtnReset                              ;=== Reset the Timer to 00:00
            $_CompteArebour = 0
            GUICtrlSetData($TimerLabel, StringFormat("%02u" & ":" & "%02u" & ":" & "%02u", $_Hours, $_Minutes, $_Seconds))
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
    Local $_HourCalc = Int($_CompteArebour / (3600000)), $_MinCalc = $_CompteArebour / 60000 - ($_HourCalc * 60), $_SecCalc = $_CompteArebour - ($_MinCalc * 60 * 1000)
    $_SecCalc = Int($_SecCalc / 1000000)

    $_CompteArebour -= TimerDiff($TimeTicks)
    $TimeTicks = TimerInit()
    Local $_HourCalc = Int($_CompteArebour / (3600000)), $_MinCalc = $_CompteArebour / 60000 - ($_HourCalc * 60), $_SecCalc = $_CompteArebour - ($_MinCalc * 60 * 1000)
    $_SecCalc = Int($_SecCalc / 1000000)
    If $_HourCalc <= 0 And $_MinCalc <= 0 And $_SecCalc <= 0 Then
        GUISetBkColor(0xFF0000, $Form1)
        GUICtrlSetData($TimerLabel, "Power!")
        Sleep(1000)
        GUICtrlSetData($TimerLabel, StringFormat("%02u" & ":" & "%02u" & ":" & "%02u", $_Hours, $_Minutes, $_Seconds))
        ; If @Compiled Then Shutdown ( 13 )
    Else
        If $_HourCalc <> $_Hours Or $_MinCalc <> $_Minutes Or $_SecCalc <> $_Seconds Then
            $_Hours = $_HourCalc
            $_Minutes = $_MinCalc
            $_Seconds = $_SecCalc
            GUICtrlSetData($TimerLabel, StringFormat("%02u" & ":" & "%02u" & ":" & "%02u", $_Hours, $_Minutes, $_Seconds))
            If $_Hours = 0 And $_Minutes = 0 And $_Seconds <= 10 Then
                Beep(1200, 100)
                GUISetBkColor(0xA093FF, $Form1)
            EndIf
        EndIf
    EndIf
EndFunc   ;==>_Check

 

Link to comment
Share on other sites

First, you are Declaring/ Calculating your "times" twice:

 

Local $_HourCalc = Int($_CompteArebour / (3600000)), $_MinCalc = $_CompteArebour / 60000 - ($_HourCalc * 60), $_SecCalc = $_CompteArebour - ($_MinCalc * 60 * 1000)
    $_SecCalc = Int($_SecCalc / 1000000)

    $_CompteArebour -= TimerDiff($TimeTicks)
    $TimeTicks = TimerInit()
    Local $_HourCalc = Int($_CompteArebour / (3600000)), $_MinCalc = $_CompteArebour / 60000 - ($_HourCalc * 60), $_SecCalc = $_CompteArebour - ($_MinCalc * 60 * 1000)
    $_SecCalc = Int($_SecCalc / 1000000)

Second, There is for this:

 

Func _Run()
    While 1
        _Check()
    WEnd
EndFunc   ;==>_Run

Which, you build your GUI, then start a "normal" GUI loop.. but when someone clicks any button, it goes into the _Run() loop.  Which then calls itself when you click another button... and so on and so forth which is why your GUI isn't responding. It's chasing it's own tail.

 

Just make your calculations in your main program loop:

 

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $BtnStart                              ;<=== When pressed "Start", hide and then show "Pause"
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd60                              ;=== Add 1 minute to the Timer
            $_CompteArebour = $_CompteArebour + (60000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd30                              ;=== Add 30 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (100000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd10                              ;=== Add 10 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (120000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd5                               ;=== Add 5 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (180000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnReset                              ;=== Reset the Timer to 00:00
            $_CompteArebour = 0
            GUICtrlSetData($TimerLabel, StringFormat("%02u" & ":" & "%02u" & ":" & "%02u", 0, 0, 0))
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Make calculations here, or just call _Check() without all the fancy nonsense of another loop in _check() getting the GUIMsg.
WEnd

Third..

Case $BtnAdd60                              ;=== Add 1 minute to the Timer
            $_CompteArebour = $_CompteArebour + (60000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd30                              ;=== Add 30 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (100000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd10                              ;=== Add 10 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (120000 * 60)
            GUICtrlSetState($BtnStart, $GUI_HIDE)
            GUICtrlSetState($BtnPause, $GUI_SHOW)
            _Run()
        Case $BtnAdd5                               ;=== Add 5 seconds to the Timer
            $_CompteArebour = $_CompteArebour + (180000 * 60)

Where are you pulling these numbers from, because they seem to be out of thin air.

 

A typical Sleep for x seconds looks like:

Local $iSleepSeconds = 60
, $iSleepCounter = 0

While 1
    Sleep(1000) ;1000 is 1 second.

   $iSleepCounter += 1
   If $iSleepCounter >= $iSleepSeconds Then ExitLoop
WEnd

msgbox(0,'',"All done!.. I slept for " & $iSleepSeconds & " seconds boss.")

Then you could build in additions to sleep time with your cases:

 

Case $BtnAdd60                              ;=== Add 1 minute to the Timer
            $iSleepSeconds += 60
        Case $BtnAdd30                              ;=== Add 30 seconds to the Timer
            $iSleepSeconds += 30
        Case $BtnAdd10                              ;=== Add 10 seconds to the Timer
            $iSleepSeconds += 10
        Case $BtnAdd5                               ;=== Add 5 seconds to the Timer
            $iSleepSeconds += 5

Looks a lot prettier :)

Edited by kaisies
spaghetti code is ufn.
Link to comment
Share on other sites

Awesome thank you so much. The numbers don't match the labels I really have to get better at labeling things properly. The numbers are milliseconds*60 to equal minuets in 1 hour, 100min, 2 hours, and 3 hours. I'm not sure if it actually has to be done that way it's just how I did it in AHK. I'm going to give these a try again thank you.

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