Jump to content
Sign in to follow this  

Simple Stopwatch

Recommended Posts



I wanted to share my very simple but quite handy stopwatch that I use every day for all sorts of stuff.




  • Pause function
  • Reset function
  • Multiple instances possible



#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#AutoIt3Wrapper_Icon=D:\Bilder\icons\alarm.ico ;Icon is attached to forum post, change to correct path
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Stopwatch", 219, 87, 192, 124, -1, $WS_EX_TOPMOST)
$b_start = GUICtrlCreateButton("Start", 8, 56, 59, 25)
$b_pause = GUICtrlCreateButton("Pause", 80, 56, 59, 25)
$b_reset = GUICtrlCreateButton("Reset", 152, 56, 59, 25)
$l_timer = GUICtrlCreateLabel("00:00:00", 0, 16, 219, 33, $SS_CENTER)
GUICtrlSetFont(-1, 18, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
#EndRegion ### END Koda GUI section ###

Global $state = "Idle", $timer, $render_timer, $saved_time = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Case $b_start
        Case $b_pause
        Case $b_reset
    If $state == "Running" And TimerDiff($render_timer) >= 40 Then renderTime()

Func start_timer()
    If $state == "Idle" Then
        Global $timer = TimerInit(), $state = "Running", $render_timer = TimerInit()

Func pause_timer()
    If $state == "Running" Then
        $state = "Paused"
        $saved_time = TimerDiff($timer) + $saved_time
        GUICtrlSetData($b_pause, "Resume")
    ElseIf $state == "Paused" Then
        $state = "Running"
        $timer = TimerInit()
        GUICtrlSetData($b_pause, "Pause")

Func reset_timer()
    Global $timer = "", $state = "Idle", $render_timer = "", $saved_time = 0
    GUICtrlSetData($l_timer, "00:00:00")
    GUICtrlSetData($b_pause, "Pause")

Func renderTime()
    Local $diff = TimerDiff($timer) + $saved_time
    Local $sec = Int(Mod($diff/1000, 60))
    Local $min = Int(Mod($diff/60000, 60))
    Local $hour = Int($diff/3600000)
    If $sec < 10 Then $sec = "0"&$sec
    If $min < 10 Then $min = "0"&$min
    If $hour < 10 Then $hour = "0"&$hour
    GUICtrlSetData($l_timer, $hour&":"&$min&":"&$sec)
    $render_timer = TimerInit()





  • Like 2

Share this post

Link to post
Share on other sites

Pretty cool.  I have a couple of recommendations, features to offer.

The reset button: it's been a while since I used a real stopwatch, but I recall that reset would zero the time, but not affect the running state of the stopwatch (think lap timer).  I guess my point is, IMHO, it is not necessary to stop the timer when the reset button is pressed.  (If reset button pressed while running, restart timer; if reset button pressed while stopped, zero time)

Lap timer: you can probably infer from my previous remark.  Perhaps, collect timer snapshots with each "lap" and display them all when watch is stopped.  Just an idea.

Milliseconds: It would be neat to see milliseconds included in the display.

Also, another comment.  In your reset and start timer function, you reset your globals by re-declaring them as globals.  That's not a very good practice from what I understand.  I can see that it is easier to set them on a single line rather than set one per line, but it can lead to issues. 

Anyways, just wanted to offer some things to think about and possible coding challenges.  

Share this post

Link to post
Share on other sites

Wow, somebody actually replied!

The reset button change makes a lot of sense and will be added. I didn't encounter any issues with redeclaring globals inside functions yet but I will keep that in mind.

Thanks for your feedback! Was hoping someone would chime in.

Share this post

Link to post
Share on other sites

Hi @DarkFingers1337, this stopwatch is written as a "finished product", nice toy :), anyway, you should keep separate the background logic and the application gui so that the "stopwatch" can be used and reused even in other contexts.
Here is a very similar stopwatch I wrote some time ago where the logic is written as a standalone function, and the gui is a separate "section" that make use of the reusable standalone "stopwatch engine".

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post

Link to post
Share on other sites

Very cool example @Chimp, yes my stopwatch is intended to be a finished product. I have it running most of the time to measure work time etc. I actually have it running right now!

When I started working on this app, I just needed something quickly to measure time and as I wasn't satisfied with any stopwatches out there I decided to make my own. Cool Timer was a good app I used a lot but new versions ruined it for me.

Edited by DarkFingers1337

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