Jump to content

Timer function


Recommended Posts

Hi!

Based on this topic I managed to put together this GUI but I don't know:

  • How to use _Counter() multiple times within _Process() with different values like  _Counter($time, $labelwhenfinished) ?
  • How to create a proper pause function?
  • How to calculate elapsed time?

Any kind of help is appreciated!

 

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


Global $Paused
Global $Timefrag = 3000
Global $TimeLimit = $Timefrag & "000"
Global $CountDown = $TimeLimit, $_Minutes, $_Seconds


HotKeySet("{PAUSE}", "_Pause")


#Region GUI
Global $_GuiCountDown = GUICreate("Timer", 356, 130)
Global $TimeLabel = GUICtrlCreateLabel("", 21, 10, 313, 100, $SS_CENTER)
GUICtrlSetFont($TimeLabel, 80, 800)
Global $Elapsed = GUICtrlCreateLabel("00:00", 241, 10, 94, 17, $SS_CENTER)
GUICtrlSetFont($Elapsed, 8, 800, 0, "MS Sans Serif")
GUICtrlSetTip($Elapsed, "Elapsed Time")
GUISetState(@SW_SHOW)
Global $TimeTicks = TimerInit()
#EndRegion GUI


While 1
    _Counter()
    Sleep(200)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd


#Region Functions
Func _Counter()
    $CountDown -= TimerDiff($TimeTicks)
    $TimeTicks = TimerInit()
    Local $_MinCalc = Int($CountDown / (60 * 1000)), $_SecCalc = $CountDown - ($_MinCalc * 60 * 1000)
    $_SecCalc = Int($_SecCalc / 1000)


    If $_MinCalc <= 0 And $_SecCalc <= 0 Then
        GUISetBkColor(0xFF7E2F, $_GuiCountDown)
        GUICtrlSetData($TimeLabel, "00:00")
        GUICtrlSetData($Elapsed, "Break!")
        Beep(1200, 2000)


    Else
        If $_MinCalc <> $_Minutes Or $_SecCalc <> $_Seconds Then
            $_Minutes = $_MinCalc
            $_Seconds = $_SecCalc
            GUICtrlSetData($TimeLabel, StringFormat("%02u" & ":" & "%02u", $_Minutes, $_Seconds))
            If $_Minutes = 0 And $_Seconds <= 10 Then
                Beep(1200, 100)
                GUISetBkColor(0x28CC7A, $_GuiCountDown)
            EndIf
        EndIf
    EndIf
EndFunc   ;==>_Counter


Func _Process()
    _Counter ; 50 min
; _Counter 10 min while GUISetBkColor(0xFF7E2F, $_GuiCountDown) and GUICtrlSetData($Elapsed, "Fin!") when finished
    _Counter ; 50 min
; _Counter 10 min while GUISetBkColor(0xFF7E2F, $_GuiCountDown) and GUICtrlSetData($Elapsed, "Fin!") when finished
    _Counter ; 50 min
; _Counter 20 min while GUISetBkColor(0xFF7E2F, $_GuiCountDown) and GUICtrlSetData($Elapsed, "Fin!") when finished
; ...

EndFunc   ;==>_Process


Func _Pause()
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
        GUICtrlSetData($Elapsed, "Paused")
    WEnd
EndFunc   ;==>_Pause
#EndRegion Functions

 

Edited by SirAlonne
Link to comment
Share on other sites

Learn about function parameters and the syntax used to create them.

#include <MsgBoxConstants.au3>

Local $iVariable = Addition(7, 2)
MsgBox($MB_OK, "Add Parameters", $iVariable)

Func Addition($iParam_A, $iParam_B) ; example function with two parameters
    Return $iParam_A + $iParam_B ; add the parameters together and return the result
EndFunc ;==> Addition

Then rewrite the function _Counter() and add the parameters you need to do what you want.
 

Edited by czardas
Link to comment
Share on other sites

@czardas

I fixed it but the _pause() is still not working so _Counter() is just keeps running in the background even when paused.

 

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


Global $Paused


HotKeySet("{PAUSE}", "_Pause")
HotKeySet("{ESC}", "_Leave")


#Region GUI
Global $_GuiCountDown = GUICreate("Timer", 356, 130)
Global $TimeLabel = GUICtrlCreateLabel("", 21, 10, 313, 100, $SS_CENTER)
GUICtrlSetFont($TimeLabel, 80, 800)
Global $Elapsed = GUICtrlCreateLabel("", 241, 10, 94, 17, $SS_CENTER)
GUICtrlSetFont($Elapsed, 8, 800, 0, "MS Sans Serif")
GUICtrlSetTip($Elapsed, "Elapsed Time")
GUISetState(@SW_SHOW)
#EndRegion GUI


While 1
    _Process()
    Sleep(200)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd


#Region Functions
Func _Counter($Timefrag, $LabelFin, $ColorFin)
    Local $TimeTicks = TimerInit()
    Local $TimeLimit = ($Timefrag * 60) * 1000
    Local $CountDown = $TimeLimit, $_Minutes, $_Seconds
    Local $tick
    Local $CountUp = $tick, $_Elmin, $_Elsec
    If Not $ColorFin Then $ColorFin = "0xf0f0f0"


    While 1
        $CountDown -= TimerDiff($TimeTicks)
        $CountUp += TimerDiff($TimeTicks)
        $TimeTicks = TimerInit()
        Local $_MinCalc = Int($CountDown / (60 * 1000)), $_SecCalc = $CountDown - ($_MinCalc * 60 * 1000)
        $_SecCalc = Int($_SecCalc / 1000)


        Local $_ElMinCalc = Int($CountUp / (60 * 1000)), $_ElSecCalc = $CountUp - ($_ElMinCalc * 60 * 1000)
        $_ElSecCalc = Int($_ElSecCalc / 1000)


        If $_MinCalc <= 0 And $_SecCalc <= 0 Then
            GUISetBkColor($ColorFin, $_GuiCountDown)
            GUICtrlSetData($TimeLabel, "00:00")
            GUICtrlSetData($Elapsed, $LabelFin)
            Beep(1200, 2000)
            ExitLoop


        Else
            If $_MinCalc <> $_Minutes Or $_SecCalc <> $_Seconds Then
                $_Minutes = $_MinCalc
                $_Seconds = $_SecCalc
                GUICtrlSetData($TimeLabel, StringFormat("%02u" & ":" & "%02u", $_Minutes, $_Seconds))
                If $_Minutes = 0 And $_Seconds <= 10 Then
                    Beep(1200, 100)
                    GUISetBkColor(0x28CC7A, $_GuiCountDown)
                EndIf
            EndIf
            If $_ElMinCalc <> $_Elmin Or $_ElSecCalc <> $_Elsec Then
                $_Elmin = $_ElMinCalc
                $_Elsec = $_ElSecCalc
                GUICtrlSetData($Elapsed, StringFormat("%02u" & ":" & "%02u", $_Elmin, $_Elsec))
            EndIf
        EndIf
    WEnd
EndFunc   ;==>_Counter



Func _Process()
    _Counter(0.2, "Break!", "0xFF7E2F")
    _Counter(2, "Test", "")


EndFunc   ;==>_Process


Func _Pause()
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
        GUICtrlSetData($Elapsed, "Paused")
    WEnd
EndFunc   ;==>_Pause


Func _leave()
    Exit
EndFunc   ;==>_leave
#EndRegion Functions

 

Link to comment
Share on other sites

2 hours ago, SirAlonne said:

I fixed it but the _pause() is still not working so _Counter() is just keeps running in the background even when paused.

Yes it makes sense since TimerInit () is not paused during your _Pause () function.  In order to achieve what you want, you will have to keep a counter of the TimerDiff () at the start of the pause and reinit the Timer after pause is unpaused.  Then add the counterto the new TimerDiff ().  Hope it is clear enough :)

Link to comment
Share on other sites

Thanks, but to tell you the truth.. @Nine ~

  • How can I keep a counter of the TimerDiff() at the start of _Pause()? You mean the _Counter() function itselft or create a new variable with TimerDiff()?
  • How to reinit the timer?
  • New TimerDiff()? So there should be a new one?

Sorry, I can't even ask proper questions here. >_< 

Link to comment
Share on other sites

Here something like that :

Func _Pause()
    $Paused = Not $Paused
    If $Paused then
      $TimerTotal += TimerDiff ($TimeTicks) ; Keep total of timer
      GUICtrlSetData($Elapsed, "Paused")
    Else
      $TimeTicks = TimerInit ()
    EndIf
    While $Paused
        Sleep(100)
    WEnd
    
EndFunc   ;==>_Pause

You must declare those variables as Global  at the beginning of the script.

Global $TimeTicks, $TimerTotal = 0

Now last changes, you must add $TimerTotal to elapsed times, and you must reset $TimerTotal = 0 when you do a new TimerInit ()

Link to comment
Share on other sites

@Nine

Thanks for your solution!
I overcomplicated the whole thing because I was too focused on the variables, instead of the loop and TimerInit() TimerDiff() relation.. Now even your first answer makes sense.:sweating:

By the way, I think in case of elapsed time $TimerTotal is not necessary because $CountUp using the same $TimeTicks.

 

Edited by SirAlonne
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...