Jump to content
Sign in to follow this  
Ugur Yuvarlak

help with a timer function

Recommended Posts

Ugur Yuvarlak

Hello friends,

lately I have written a script to use in time keeping works. script is quite simple, just a time period in seconds is entered, and it starts to count down, while showing the remaining time in seconds on the read only text box.

but to do this all I explained, I have written this script below:

#include <guiconstants.au3>

#include <comaudio.au3>

#no tray icon

$gong=$device.opensound("gong.wav", true)

while 1

$timer=inputbox("timer", "Enter a time period in seconds:", "", "", -1, -1, 0, 0)

select

case $timer=""

exit

case stringisdigit($timer)=0

msgbox(0, "ERROR!", $timer & " is not containing a valid time period in secondd, try again.")

$timer=inputbox("timer", "Enter a time period in seconds:", "", "", -1, -1, 0, 0)

case stringisdigit($timer)=1

clock($timer)

endselect

wend

func clock($label)

$gui=guicreate("TIMER", 300, 180)

$edit=GuiCtrlCreateEdit("", -1, -1, 100, 25, $es_readonly)

guisetstate(@sw_show, $gui)

for $t = $label to 0 step -1

GuiCtrlSetData($edit, $t, "")

sleep(998)

next

$gong.play

GuiDelete($gui)

endfunc()

this script works perfect, the only problem is when it starts to count down, there is no other way than god or windows task maneger can stop it. Because I have used sleep(998) (998 with considering the procession period of script) I can't make the gui check the guigetmsg() so script never knows if there is something like this. for a while I thought if I should use gui on event mode but I am not a script master yet and would you give me an idea about what I can do further?

thanks for your interest,

Ugur

Share this post


Link to post
Share on other sites
SmOke_N

Why not stick a loop after the For/Next in the function that checks for GUIGetMsg()?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
PsaltyDS

Another classic call for GuiOnEventMode... going off and doing something else (updating the timer) without having to be locked into a GuiGetMsg() loop:

#include <guiconstants.au3>

Opt("GuiOnEventMode", 1)

Global $hGUI

While 1
    $timer = InputBox("timer", "Enter a time period in seconds:", "", "", -1, -1, 0, 0)
    Select
        Case @error Or $timer = ""
            Exit
        Case StringIsDigit($timer) = 0
            MsgBox(0, "ERROR!", $timer & " is not containing a valid time period in secondd, try again.")
            $timer = InputBox("timer", "Enter a time period in seconds:", "", "", -1, -1, 0, 0)
        Case StringIsDigit($timer) = 1
            clock($timer)
    EndSelect
WEnd

Func clock($label)
    $hGUI = GUICreate("TIMER", 300, 180)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseGUI")
    Local $edit = GUICtrlCreateEdit($label, -1, -1, 100, 25, $es_readonly)
    GUISetState(@SW_SHOW, $hGUI)

    Local $iTimer = TimerInit()
    While WinExists($hGUI)
        If TimerDiff($iTimer) > 1000 Then
            $label -= 1
            GUICtrlSetData($edit, $label)
            $iTimer = TimerInit()
        EndIf
        If $label <= 0 Then _CloseGUI()
        Sleep(20)
    WEnd
    Beep(1000, 1000)
EndFunc   ;==>clock

Func _CloseGUI()
    GUIDelete($hGUI)
EndFunc   ;==>_CloseGUI

;)

Edit: Oops, my copy/paste clipped the top couple of lines that included guiconstants.au3 and set GuiOnEventMode option... :)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
therks

Actually, I can't close your example while the timer is counting down Psalty... not sure why but I didn't examine closely.

Coincidently enough I just recently wrote my own timer script for work.

I do patrols of a building, and between patrols I fiddle around on my laptop (usually AutoIt, heh), but I lose track of time like mad, so I wrote this.

I had a unique way of closing it, since I didn't want to close it accidentally. When you hit the [x] it will minimize to the tray, you can right click the tray icon to either show the window again, or close the program (there's also an item showing how much time left in the countdown). I also added a context menu to the Stop button, it has an exit item too so you don't have to use the tray menu.

Hopefully you can get some ideas for your own script from mine.

#include <GUIConstants.au3>

Global $iTime = 0, $iRealTimer = 0
Global $iSS_FULLCENTER = BitOR($SS_CENTER, $SS_CENTERIMAGE)

Opt('TrayMenuMode', 1)
TraySetClick(8)
TraySetToolTip('Timer')

$ti_Show = TrayCreateItem('&Show')
    TrayItemSetState(-1, 512) ; $TRAY_DEFAULT
$ti_Exit = TrayCreateItem('E&xit')
TrayCreateItem('')
$ti_Time = TrayCreateItem('00:00:00')
    TrayItemSetState(-1, 128) ; $TRAY_DISABLE

$gui = GUICreate('Timer', 100, 55, Default, Default, Default, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUICtrlCreateLabel('&HH', 0, 0, 20, 15, $iSS_FULLCENTER)
GUICtrlCreateLabel(':', 20, 0, 5, 15, $iSS_FULLCENTER)
$in_Hour = GUICtrlCreateInput('00', 0, 15, 20, 20, $ES_RIGHT)
GUICtrlCreateLabel(':', 20, 15, 5, 20, $iSS_FULLCENTER)

GUICtrlCreateLabel('&MM', 25, 0, 20, 15, $iSS_FULLCENTER)
GUICtrlCreateLabel(':', 45, 0, 5, 15, $iSS_FULLCENTER)
$in_Min = GUICtrlCreateInput('00', 25, 15, 20, 20, $ES_RIGHT)
GUICtrlCreateLabel(':', 45, 15, 5, 20, $iSS_FULLCENTER)

GUICtrlCreateLabel('&SS', 50, 0, 20, 15, $iSS_FULLCENTER)
GUICtrlCreateLabel('.', 70, 0, 5, 15, $iSS_FULLCENTER)
$in_Sec = GUICtrlCreateInput('00', 50, 15, 20, 20, $ES_RIGHT)
GUICtrlCreateLabel('.', 70, 15, 5, 20, $iSS_FULLCENTER)

GUICtrlCreateLabel('MS', 75, 0, 25, 15, $iSS_FULLCENTER)
$in_MS = GUICtrlCreateInput('000', 75, 15, 25, 20, $ES_RIGHT)

$bt_Start = GUICtrlCreateButton('St&art', 0, 35, 50, 20)
    GUICtrlSetState(-1, $GUI_DEFBUTTON)
$bt_Stop = GUICtrlCreateButton('S&top', 50, 35, 50, 20)
    GUICtrlSetState(-1, $GUI_DISABLE)
    $cm_Stop = GUICtrlCreateContextMenu(-1)
    $mi_Exit = GUICtrlCreateMenuItem('E&xit', $cm_Stop)

GUISetState()

While 1
    $tm = TrayGetMsg()
    Switch $tm
        Case $ti_Show
            GUISetState(@SW_SHOW)
            WinActivate($gui)
            TraySetState(8)
        Case $ti_Exit
            Exit
    EndSwitch
    
    $gm = GUIGetMsg()
    Switch $gm
        Case $bt_Start
            $iHour = Int(GUICtrlRead($in_Hour))
            $iMin = Int(GUICtrlRead($in_Min))
            $iSec = Int(GUICtrlRead($in_Sec))
            $iMS = Int(GUICtrlRead($in_MS))
            $iTime = ($iHour * 3600 + $iMin * 60 + $iSec) * 1000 + $iMS
            $iRealTimer = TimerInit()
            _ToggleControls(0)
            AdlibEnable('_Countdown', 5)
        Case $bt_Stop
            AdlibDisable()
            _ToggleControls(1)
        Case $GUI_EVENT_CLOSE
            GUISetState(@SW_MINIMIZE)
            GUISetState(@SW_HIDE)
            TraySetState(4)
        Case $mi_Exit
            Exit
    EndSwitch
WEnd

Func _Countdown()
    $iTimeDiff = $iTime - Round(TimerDiff($iRealTimer))

    If $iTimeDiff < 0 Then $iTimeDiff = 0

    $iHour = _StringPadLeft(Floor($iTimeDiff / 3600000), '0', 2)
    $iMin = _StringPadLeft(Floor(Mod($iTimeDiff, 3600000) / 60000), '0', 2)
    $iSec = _StringPadLeft(Floor(Mod($iTimeDiff, 60000) / 1000), '0', 2)
    $iMS = _StringPadLeft(Mod($iTimeDiff, 1000), '0', 3)
    
    GUICtrlSetData($in_Hour, $iHour)
    GUICtrlSetData($in_Min, $iMin)
    GUICtrlSetData($in_Sec, $iSec)
    GUICtrlSetData($in_MS, $iMS)
    
    $sTimeString = $iHour & ':' & $iMin & ':' & $iSec
    TraySetToolTip($sTimeString)
    TrayItemSetText($ti_Time, $sTimeString)
    
    If $iTimeDiff = 0 Then
        AdlibDisable()
        _ToggleControls(1)
        GUISetState(@SW_SHOW)
        WinActivate($gui)
        TraySetState(8)
        SoundPlay('C:\windows\media\tada.wav', 0)
    EndIf
EndFunc

Func _StringPadLeft($sString, $sPad, $iLen)
    While StringLen($sString) < $iLen
        $sString = $sPad & $sString
    WEnd
    Return $sString
EndFunc

Func _ToggleControls($bOnOff)
    Local $aCtrlSet[5] = [ $bt_Start, $in_Hour, $in_Min, $in_Sec, $in_MS ]
    If $bOnOff Then
        $iCtrlSet = $GUI_ENABLE
        $iCtrlOdd = $GUI_DISABLE
        $iCtrlFocus = $bt_Start
    Else
        $iCtrlSet = $GUI_DISABLE
        $iCtrlOdd = $GUI_ENABLE
        $iCtrlFocus = $bt_Stop
    EndIf
    
    For $i = 0 To 4
        GUICtrlSetState($aCtrlSet[$i], $iCtrlSet)
    Next
    GUICtrlSetState($bt_Stop, $iCtrlOdd)
    ControlFocus($gui, '', $iCtrlFocus)
EndFunc

Share this post


Link to post
Share on other sites
PsaltyDS

Actually, I can't close your example while the timer is counting down Psalty... not sure why but I didn't examine closely.

Oops... when I copy/pasted into the post, I clipped the top couple of lines, including the Opt("GuiOnEventMode", 1).

:">


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
therks

Oh, duh. Can't believe I missed that myself. Obviously if you said it was going to use OnEvent mode, I should have made note that it wasn't turned on. Sorry.

Share this post


Link to post
Share on other sites
oscar

Hi, this is Saunders nice Timer plus some fix and new features like updown arrows for easily setting time and a Clear Entry button.

; http://www.autoitscript.com/forum/index.ph...34&hl=timer

#include <GUIConstants.au3>

Global $iTime = 0, $iRealTimer = 0

Global $iSS_FULLCENTER = BitOR($SS_CENTER, $SS_CENTERIMAGE)

Opt('TrayMenuMode', 1)

TraySetClick(8)

TraySetToolTip('auTimer')

$ti_Show = TrayCreateItem('&Show')

TrayItemSetState(-1, 512) ; $TRAY_DEFAULT

$ti_Exit = TrayCreateItem('E&xit')

TrayCreateItem('')

$ti_Time = TrayCreateItem('00:00:00')

TrayItemSetState(-1, 128) ; $TRAY_DISABLE

$gui = GUICreate('auTimer', 180, 55, Default, Default, Default, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))

GUICtrlCreateLabel('&HH:', 3, 0, 20, 15, $iSS_FULLCENTER)

;GUICtrlCreateLabel(':', 20, 0, 5, 15, $iSS_FULLCENTER)

$in_Hour = GUICtrlCreateInput('00', 0, 15, 40, 20, $ES_RIGHT)

GUICtrlCreateLabel(':', 40, 15, 5, 20, $iSS_FULLCENTER)

$updown = GUICtrlCreateUpdown($in_Hour)

GUICtrlCreateLabel('&MM:', 49, 0, 20, 15, $iSS_FULLCENTER)

;GUICtrlCreateLabel(':', 45, 0, 5, 15, $iSS_FULLCENTER)

$in_Min = GUICtrlCreateInput('00', 45, 15, 40, 20, $ES_RIGHT)

GUICtrlCreateLabel(':', 85, 15, 5, 20, $iSS_FULLCENTER)

$updown = GUICtrlCreateUpdown($in_Min)

GUICtrlCreateLabel(' &SS.', 92, 0, 20, 15, $iSS_FULLCENTER)

;GUICtrlCreateLabel('.', 70, 0, 5, 15, $iSS_FULLCENTER)

$in_Sec = GUICtrlCreateInput('00', 90, 15, 40, 20, $ES_RIGHT)

GUICtrlCreateLabel('.', 130, 15, 5, 20, $iSS_FULLCENTER)

$updown = GUICtrlCreateUpdown($in_Sec)

GUICtrlCreateLabel('MS', 135, 0, 25, 15, $iSS_FULLCENTER)

$in_MS = GUICtrlCreateInput('000', 135, 15, 45, 20, $ES_RIGHT)

$updown = GUICtrlCreateUpdown($in_MS)

$bt_Start = GUICtrlCreateButton('St&art', 0, 35, 40, 20)

GUICtrlSetState(-1, $GUI_DEFBUTTON)

$bt_Stop = GUICtrlCreateButton('S&top', 45, 35, 40, 20)

GUICtrlSetState(-1, $GUI_DISABLE)

$bt_Reset = GUICtrlCreateButton('&CE', 150, 35, 30, 20)

GUICtrlSetState(-1, $GUI_DEFBUTTON)

$cm_Stop = GUICtrlCreateContextMenu(-1)

$mi_Exit = GUICtrlCreateMenuItem('E&xit', $cm_Stop)

GUISetState()

While 1

$tm = TrayGetMsg()

Switch $tm

Case $ti_Show

GUISetState(@SW_MAXIMIZE)

WinActivate($gui)

TraySetState(8)

Case $ti_Exit

Exit

EndSwitch

$gm = GUIGetMsg()

Switch $gm

Case $bt_Start

$iHour = Int(GUICtrlRead($in_Hour))

$iMin = Int(GUICtrlRead($in_Min))

$iSec = Int(GUICtrlRead($in_Sec))

$iMS = Int(GUICtrlRead($in_MS))

$iTime = ($iHour * 3600 + $iMin * 60 + $iSec) * 1000 + $iMS

$iRealTimer = TimerInit()

_ToggleControls(0)

AdlibEnable('_Countdown', 5)

$chimes = 3

Case $bt_Reset

$iHour = 0

$iMin = 0

$iSec = 0

$iMS = 0

$iTime = ($iHour * 3600 + $iMin * 60 + $iSec) * 1000 + $iMS

;_ToggleControls(1)

AdlibEnable('_Countdown', 5)

$chimes = -1

Case $bt_Stop

AdlibDisable()

_ToggleControls(1)

Case $GUI_EVENT_CLOSE

GUISetState(@SW_MINIMIZE)

GUISetState(@SW_HIDE)

TraySetState(4)

Case $mi_Exit

Exit

EndSwitch

WEnd

Func _Countdown()

$iTimeDiff = $iTime - Round(TimerDiff($iRealTimer))

If $iTimeDiff < 0 Then $iTimeDiff = 0

$iHour = _StringPadLeft(Floor($iTimeDiff / 3600000), '0', 2)

$iMin = _StringPadLeft(Floor(Mod($iTimeDiff, 3600000) / 60000), '0', 2)

$iSec = _StringPadLeft(Floor(Mod($iTimeDiff, 60000) / 1000), '0', 2)

$iMS = _StringPadLeft(Mod($iTimeDiff, 1000), '0', 3)

GUICtrlSetData($in_Hour, $iHour)

GUICtrlSetData($in_Min, $iMin)

GUICtrlSetData($in_Sec, $iSec)

GUICtrlSetData($in_MS, $iMS)

$sTimeString = $iHour & ':' & $iMin & ':' & $iSec

TraySetToolTip($sTimeString)

TrayItemSetText($ti_Time, $sTimeString)

If $iTimeDiff = 0 Then

AdlibDisable()

_ToggleControls(1)

GUISetState(@SW_SHOW)

WinActivate($gui)

TraySetState(8)

For $i = 0 To $chimes

SoundPlay(@WindowsDir & "\media\chimes.wav",1)

next

EndIf

EndFunc

Func _StringPadLeft($sString, $sPad, $iLen)

While StringLen($sString) < $iLen

$sString = $sPad & $sString

WEnd

Return $sString

EndFunc

Func _ToggleControls($bOnOff)

Local $aCtrlSet[5] = [ $bt_Start, $in_Hour, $in_Min, $in_Sec, $in_MS ]

If $bOnOff Then

$iCtrlSet = $GUI_ENABLE

$iCtrlOdd = $GUI_DISABLE

$iCtrlFocus = $bt_Start

Else

$iCtrlSet = $GUI_DISABLE

$iCtrlOdd = $GUI_ENABLE

$iCtrlFocus = $bt_Stop

EndIf

For $i = 0 To 4

GUICtrlSetState($aCtrlSet[$i], $iCtrlSet)

Next

GUICtrlSetState($bt_Stop, $iCtrlOdd)

ControlFocus($gui, '', $iCtrlFocus)

EndFunc

Share this post


Link to post
Share on other sites
L3G3NdKillEr

Hi, this is Saunders nice Timer plus some fix and new features like updown arrows for easily setting time and a Clear Entry button.

Nice Work :)


i'm very responsible, when ever something goes wrong they always say I'm responsible.Life is like an Adventure... BUT COOL GRAPHICS<====================----=LEGEND KILLER=----=========================>

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.