Jump to content

Stupid little timer.


bigred
 Share

Recommended Posts

I'm working on this little timer app. I'm not really much of a programmer but I have done a fair amount with VB6 in the past.

What I really was looking for was the Timer functionality of VB6. Whats nice about it is that you can let the timer run in the backgroud while your code executes other things. The problem I'm having is that while my program is waiting for 1 minute to pass with the Sleep command running on a 1 second time out loop, my program is unresponsive until the loop completes.

This is what I have so far. Obviously nowhere near done, but since I hit the big problem of the program being unresponsive while the timer is counting up in the while loop I figuered I'd see what you guys had to say.

#include <GuiConstants.au3>

        GuiCreate("Timer", 160, 70)
        $hour = "00"
        $min = "00"
        $sec = "00"
        GUICtrlCreateLabel("Timer:", 3, 2)
        GUICtrlCreateLabel($hour & ":" & $min & ":" & $sec, 36, 2)
        $Strt_Btn = GuiCtrlCreateButton("Start", 10, 40, 70)
        $Stop_Btn = GuiCtrlCreateButton("Stop", 80, 40, 70)
        GuiSetState(@SW_SHOW)
        $count = 1
        
        While 1
           $msg = GUIGetMsg()
           Select
              Case $msg = $GUI_EVENT_CLOSE
                  GUIDelete()
                  Exit
              Case $msg = $Strt_Btn
              While $count <= 6
                Sleep (1000)
                $count = $count + 1
                $sec = $sec + 1
                If $sec = 60 Then
                  $min = $min + 1
                  GUICtrlCreateLabel($hour & ":" & $min & ":" & $sec, 36, 2)
                Else
                  If $sec <= 9 Then
                    $sec = "0" & $sec
                    GUICtrlCreateLabel($hour & ":" & $min & ":" & $sec, 36, 2)
                  Else
                    GUICtrlCreateLabel($hour & ":" & $min & ":" & $sec, 36, 2)
                  EndIf  
                EndIf
              Wend
              Case $msg = $Stop_Btn
                  GUIDelete()
                  Exit
            EndSelect  
        WEnd
Link to comment
Share on other sites

I think I remember reading somewhere (probably the helpfile) that GUIGetMsg() adds in its own delay so it doesn't eat as much CPU, and so you don't need to add in your own sleep. If this is true, and it is more than just a few ms, that will affect how accurate your timer is. One way to get around this would be to use AdlibEnable ("UpdateTimer", 1000) which would run the updatetimer function every second (you still have to create it). You could also use the system time (macros @hour, @min, @sec) and just let it update as often as it wants.

Link to comment
Share on other sites

u might want to check out this one below

forgot who did it, but it's nicely done

#include <GUIConstants.au3>
#include <Date.au3>

Dim $Header_1 = "Machine"
Dim $Header_2 = "Line"
Dim $Header_3 = "Office"
Dim $Name, $TotalTime, $Unit, $count = 33
Dim $TimerActive_[50], $Label_[50], $TButton_[50], $SButton_[50], $Input_[50], $Label_[50]
Dim $Time_[50], $Timer_[50], $sTime_[50], $xk, $ck, $Left = 0, $Top = 20, $X

$Today_File = @MON & "-" & @MDAY & "-" & @YEAR & ".txt"
$Log = FileOpen($Today_File, 1)
FileWriteLine($Log, "Logfile started: " & _DateTimeFormat( _NowCalc(), 0) & @CRLF & @CRLF)
FileClose($Log)

$Toy_Logo = @TempDir & "\Toy2-logo.jpg"
FileInstall("C:\XPClean-web\Settings\XPClean-pics\Toy-box2-jpg.jpg", $Toy_Logo)
$Toy_Banner = @TempDir & "\Toy-banr.jpg"
FileInstall("C:\XPClean-web\Settings\XPClean-pics\Toy-box-jpg.jpg", $Toy_Banner)
$Logo_icon = @TempDir & "\Toy-Icon.ico"
FileInstall("C:\XPClean-web\Settings\XPClean-pics\Toy-box-Icon.ico", $Logo_icon)

AdlibEnable("AllTimers", 500)

GUICreate("   Toy BOX  -  Multi-Station-Timer", 981, 470)
GUISetIcon($Logo_icon)
$Icon_1 = GUICtrlCreatePic($Toy_Banner, 700, 370, 210, 90)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetCursor(-1, 0)
GUICtrlSetTip(-1, "Click here - to Donate Now!!")

;Top computer label
GUICtrlCreateLabel($Header_1 & "s", 0, 0, 980, 20, $SS_CENTER, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 12, 700)
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x876B53)

;Middle phones label
GUICtrlCreateLabel($Header_2 & "s", 0, 220, 980, 20, $SS_CENTER, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 12, 700)
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x876B53)

;Bottom PC label
GUICtrlCreateLabel($Header_3 & "s", 0, 340, 980, 20, $SS_CENTER, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 12, 700)
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x876B53)

For $X = 1 To 9
; row 1
    GUICtrlCreateLabel($Header_1 & " " & $X, $Left, $Top, 100, 20, $SS_CENTER)
    $Label_[$X] = GUICtrlCreateLabel("", $Left, $Top + 20, 100, 30, 0x1000)
    GUICtrlSetFont($Label_[$X], 16)
    GUICtrlSetData($Label_[$X], "00:00:00")
    $TButton_[$X] = GUICtrlCreateButton("Start", $Left, $Top + 50, 50, 20)
    $SButton_[$X] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 50, 50, 20)
    $Input_[$X] = GUICtrlCreateInput("", $Left, $Top + 70, 100, 20, 0x1000)
    
; row 2
    GUICtrlCreateLabel($Header_1 & " " & $X + 9, $Left, $Top + 100, 100, 20, $SS_CENTER)
    $Label_[$X + 9] = GUICtrlCreateLabel("", $Left, $Top + 120, 100, 30, 0x1000)
    GUICtrlSetFont($Label_[$X + 9], 16)
    GUICtrlSetData($Label_[$X + 9], "00:00:00")
    $TButton_[$X + 9] = GUICtrlCreateButton("Start", $Left, $Top + 150, 50, 20)
    $SButton_[$X + 9] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 150, 50, 20)
    $Input_[$X + 9] = GUICtrlCreateInput("", $Left, $Top + 170, 100, 20, 0x1000)
    
; row 3
    GUICtrlCreateLabel($Header_2 & " " & $X, $Left, $Top + 220, 100, 20, $SS_CENTER)
    $Label_[$X + 18] = GUICtrlCreateLabel("", $Left, $Top + 240, 100, 30, 0x1000)
    GUICtrlSetFont($Label_[$X + 18], 16)
    GUICtrlSetData($Label_[$X + 18], "00:00:00")
    $TButton_[$X + 18] = GUICtrlCreateButton("Start", $Left, $Top + 270, 50, 20)
    $SButton_[$X + 18] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 270, 50, 20)
    $Input_[$X + 18] = GUICtrlCreateInput("", $Left, $Top + 290, 100, 20, 0x1000)
    
; row 4
    If $X < 7 Then
        GUICtrlCreateLabel($Header_3 & " " & $X, $Left, $Top + 340, 100, 20, $SS_CENTER)
        $Label_[$X + 27] = GUICtrlCreateLabel("", $Left, $Top + 360, 100, 30, 0x1000)
        GUICtrlSetFont($Label_[$X + 27], 16)
        GUICtrlSetData($Label_[$X + 27], "00:00:00")
        $TButton_[$X + 27] = GUICtrlCreateButton("Start", $Left, $Top + 390, 50, 20)
        $SButton_[$X + 27] = GUICtrlCreateButton("Stop", $Left + 50, $Top + 390, 50, 20)
        $Input_[$X + 27] = GUICtrlCreateInput("", $Left, $Top + 410, 100, 20, 0x1000)
    EndIf
    $Left = $Left + 110
Next

GUISetState()

While 1
    $msg = GUIGetMsg()
    
    For $xk = 1 To $count
        If $msg = $TButton_[$xk] And GUICtrlRead($Input_[$xk]) > "" Then
            GUICtrlSetState($TButton_[$xk], $GUI_DISABLE)
            GUICtrlSetState($Input_[$xk], $GUI_DISABLE)
            $TimerActive_[$xk] = 1
            $Timer_[$xk] = TimerInit()
        ElseIf $msg = $TButton_[$xk] Then
            MsgBox(64, "User Error", "Please Type in a User Name   ", 3)
        EndIf
        If $msg = $SButton_[$xk] And GUICtrlRead($SButton_[$xk]) = "Reset" Then
            GUICtrlSetData($Label_[$xk], "00:00:00")
            GUICtrlSetData($Input_[$xk], "")
            GUICtrlSetData($SButton_[$xk], "Stop")
            GUICtrlSetState($TButton_[$xk], $GUI_ENABLE)
            GUICtrlSetState($Input_[$xk], $GUI_ENABLE)
        EndIf
        If $msg = $SButton_[$xk] And GUICtrlRead($Input_[$xk]) > "" And GUICtrlRead($SButton_[$xk]) = "Stop" Then
            $TimerActive_[$xk] = 0
            GUICtrlSetData($SButton_[$xk], "Reset")
            
      ;
            GUICtrlSetColor($Label_[$xk], 0x000000)
            $Name = GUICtrlRead($Input_[$xk])
      ;
            If $xk < 19 Then
                $Unit = $Header_1 & "  " & $xk
            EndIf
            If $xk >= 19 And $xk < 28 Then
                $Unit = $Header_2 & "   " & $xk - 18
            EndIf
            If $xk >= 28 Then
                $Unit = $Header_3 & "   " & $xk - 27
            EndIf
            RecordStuff()
        EndIf
        
        If $xk = 33 Then ExitLoop
    Next
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func AllTimers()
    Local $Secs, $Mins, $Hour
    For $ck = 1 To $count
        
        If $TimerActive_[$ck] Then
            _TicksToTime(Int(TimerDiff($Timer_[$ck])), $Hour, $Mins, $Secs)
            $Time_[$ck] = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
            If $sTime_[$ck] <> $Time_[$ck] Then GUICtrlSetData($Label_[$ck], $Time_[$ck])
            If $Mins > 29 Then GUICtrlSetColor($Label_[$ck], 0xff0000)
        EndIf
    Next
EndFunc;==>AllTimers

Func RecordStuff()
    $LogStuff = FileOpen($Today_File, 1)
    FileWriteLine($LogStuff, "Station: " & $Unit & @CRLF)
    FileWriteLine($LogStuff, "User name: " & (GUICtrlRead($Input_[$xk])) & @CRLF)
    FileWriteLine($LogStuff, "Total time: " & (GUICtrlRead($Label_[$xk])) & @CRLF & @CRLF)
    FileClose($LogStuff)
    
EndFunc;==>RecordStuff

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

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