Jump to content

Simple counter


jakonline
 Share

Recommended Posts

i tried to make a gui counter with one button start/pause,

but when it starts, it looks refresh quickly, and cpu usage is not low,

how can i make it better ? thanks!

#include <GUIConstantsEx.au3>
$gui = GUICreate("Test", 333, 333)
$btn01=GUICtrlCreateButton("START",15,15)
GUISetFont(222 )
$lbl01 = GUICtrlCreateLabel("", 45, 53, 300, 300)
GUISetState()
Global $cnt=0, $swh=0
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn01
            $cnt +=1
            $swh=mod($cnt,2)
            $tBegin01 = TimerInit()
        EndSwitch
    If $swh=1 and TimerDiff($tBegin01) > 999 Then
        GUICtrlSetData($lbl01, int(TimerDiff($tBegin01)/1000))
    EndIf
WEnd
Edited by jakonline
Link to comment
Share on other sites

Hello jakonline,

Welcome to the AutoIt Forums!

Try adding a sleep function inside your While Loop, this will improve your cpu usage. Sleep(10) should do the trick, if still too high, try 100 milliseconds than.

Edit: typo

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

Try this, it should eliminate the flickering and I also resized the GUI and label to fit the numbers. After 99 seconds though, it won't fit on the GUI any longer because 3 digits just won't fit at that size font and that size GUI/label.

#include <GUIConstantsEx.au3>
$seconds = 0
$gui = GUICreate("Test", 360, 333)
$btn01=GUICtrlCreateButton("START",15,15)
GUISetFont(222 )
$lbl01 = GUICtrlCreateLabel("", 25, 53, 330, 300)
GUISetState()
Global $cnt=0, $swh=0
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn01
            $cnt +=1
            $swh=mod($cnt,2)
            $tBegin01 = TimerInit()
        EndSwitch
    If $swh=1 and TimerDiff($tBegin01) > 999 Then
        $seconds +=1
        GUICtrlSetData($lbl01, $seconds)
        $tBegin01 = TimerInit()
    EndIf
WEnd
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

thanks for answering,

with your suggestions so that i finally figured out what i want.

actually what i want to make is a count down counter, with the user's input time to count down itself,

the script i showed at my first post was just the main counting part of my program, so the size is not a matter ^^

the way i figured out is just to add one more TimerInit()

therefore it would be more accurate and not refreshing quickly, thank you~

#include <GUIConstantsEx.au3>
$gui = GUICreate("Test", 360, 333)
$btn01=GUICtrlCreateButton("START",15,15)
GUISetFont(222)
$lbl01 = GUICtrlCreateLabel("", 25, 53, 330, 300)
GUISetState()
Global $switch
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn01
           $switch= Not $switch
     $tBegin00 = TimerInit()
     $tBegin01 = TimerInit()
EndSwitch
    If $switch=1 and TimerDiff($tBegin01) > 999 Then
  GUICtrlSetData($lbl01, int(TimerDiff($tBegin00)/1000))
        $tBegin01 = TimerInit()
    EndIf
WEnd
Edited by jakonline
Link to comment
Share on other sites

That's not going to work as a countdown timer because it's always counting up. With the code I posted all you'd need to do to make it a countdown timer is set $seconds to the seconds you want to count down from, and change the line $seconds += 1 to $seconds -= 1 and now it will count down. There's no need to use a second timerinit if all you're doing is displaying seconds and only change the label once a second, you're over-complicating the script for zero gain.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

jakonline,

This is about as simple as it gets:

#include <GUIConstantsEx.au3>
#include <date.au3>
$gui = GUICreate("Test",400,400)
$btn01=GUICtrlCreateButton("START",100,100,200,50)
GUIctrlSetFont($btn01,24,800)
GUISetState()
Global $cnt=5, $st
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn01
            $st = _NowCalc()
            guictrlsetdata($btn01,$cnt)
        EndSwitch
    If _datediff("s",$st,_nowcalc()) > 0 then
        $cnt -= 1
        $st = _nowcalc()
        GUICtrlSetData($btn01, $cnt)
    endif
    if $cnt = 0 then
       guictrlsetdata($btn01,"Done !!!")
      sleep(3000)
     Exit
    endif
WEnd

kylomas

edit: trying to fix the indentation

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

once you can count up well, then count down would be as easy as counting up ^^

here's my original program, i was stucked on counting, so my first post was the counting part only.

#include <GUIConstantsEx.au3>
$gui = GUICreate("Count down Counter", 450, 333)
Global $left01=15,$top01=15,$width01=25,$height01=18,$intv01=$width01+10
$input01=GUICtrlCreateInput(0,$left01,$top01,$width01,$height01)
$lblh=GUICtrlCreateLabel("Hour",$left01+$intv01*1,$top01,$width01,$height01)
$input02=GUICtrlCreateInput(0,$left01+$intv01*2,$top01,$width01,$height01)
$lblm=GUICtrlCreateLabel("Min",$left01+$intv01*3,$top01,$width01,$height01)
$input03=GUICtrlCreateInput(0,$left01+$intv01*4,$top01,$width01,$height01)
$lbls=GUICtrlCreateLabel("Sec",$left01+$intv01*5,$top01,$width01,$height01)
$btn01=GUICtrlCreateButton("START",$left01+$intv01*6,$top01,$width01+30,$height01)
GUISetFont(50)
$lbl01 = GUICtrlCreateLabel("", 25, 53, 400, 300)
GUISetState()
Global $switch
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn01
           $switch = Not $switch
     $tBegin00 = TimerInit()
     $tBegin01 = TimerInit()
     $totaltime01=guictrlread($input01)*60*60+guictrlread($input02)*60+guictrlread($input03)
EndSwitch
    If $switch=1 and TimerDiff($tBegin01) > 1000 Then
  $ctime=$totaltime01-int(TimerDiff($tBegin00)/1000)
  GUICtrlSetData($lbl01,int($ctime/3600) & " :  " & int(mod($ctime,3600)/60) & " :  " & mod($ctime,60) & " ")
        $tBegin01 = TimerInit()
    EndIf
WEnd
Link to comment
Share on other sites

once you can count up well, then count down would be as easy as counting up ^^

here's my original program, i was stucked on counting, so my first post was the counting part only.

but it still need improving, i don't know how to make 1:1:1 to be 01:01:01,

and i also want to add a soundPlay function in it,

i'm now looking the the way to browse file out, and automatically save the path of sound file which user input

thanks for helping.

#include <GUIConstantsEx.au3>
$gui = GUICreate("Count down Counter", 450, 333)
Global $left01=15,$top01=15,$width01=25,$height01=18,$intv01=$width01+10
$input01=GUICtrlCreateInput(0,$left01,$top01,$width01,$height01)
$lblh=GUICtrlCreateLabel("Hour",$left01+$intv01*1,$top01,$width01,$height01)
$input02=GUICtrlCreateInput(0,$left01+$intv01*2,$top01,$width01,$height01)
$lblm=GUICtrlCreateLabel("Min",$left01+$intv01*3,$top01,$width01,$height01)
$input03=GUICtrlCreateInput(0,$left01+$intv01*4,$top01,$width01,$height01)
$lbls=GUICtrlCreateLabel("Sec",$left01+$intv01*5,$top01,$width01,$height01)
$btn01=GUICtrlCreateButton("START",$left01+$intv01*6,$top01,$width01+30,$height01)
GUISetFont(50)
$lbl01 = GUICtrlCreateLabel("", 25, 53, 400, 300)
GUISetState()
Global $switch
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn01
           $switch = Not $switch
     $tBegin00 = TimerInit()
     $tBegin01 = TimerInit()
     $totaltime01=guictrlread($input01)*60*60+guictrlread($input02)*60+guictrlread($input03)
EndSwitch
    If $switch=1 and TimerDiff($tBegin01) > 1000 Then
  $ctime=$totaltime01-int(TimerDiff($tBegin00)/1000)
  GUICtrlSetData($lbl01,int($ctime/3600) & " :  " & int(mod($ctime,3600)/60) & " :  " & mod($ctime,60) & " ")
        $tBegin01 = TimerInit()
    EndIf
WEnd
Link to comment
Share on other sites

I used this as a timer display for a media player I am working on. It will add a zero in front of any single digit in the $minutes or $seconds variables, and then you could use the result to show your time in the display as 01:01.

$Counter = StringFormat("%02u:%02u", $Minutes, $Seconds)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 4 weeks later...

An If statement would be the easiest to use.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Here 2 simple countdowns which can be modified for your needs easily.

;coded by UEZ 2011
#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)

Global $msg
Global $hGUI = GUICreate("Simple Countdown by UEZ 2011", 350, 150)
Global $label = GUICtrlCreateLabel("", 40, 30, 260, 80)
;~ GUICtrlSetBkColor(-1, 0x808080)
GUICtrlSetFont(-1, 50, 400, 0, "Arial", 4)
Global $button = GUICtrlCreateButton("Add 60 sec", 275, 120, 70)
GUISetState()

Global $countdown = 1.2 ;minutes for countdown
Global $seconds = $countdown * 60 ;convert to seconds

Countdown()
AdlibRegister("Countdown", 1000)

Do
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            $seconds += 60
    EndSwitch
Until False

AdlibUnRegister("Countdown")
GUIDelete($hGUI)
Exit

Func Countdown()
    Local $sec, $min, $hr
    $sec = Mod($seconds, 60)
    $min = Mod($seconds / 60, 60)
    $hr = Floor($seconds / 60 ^ 2)
    GUICtrlSetData($label, StringFormat("%02i:%02i:%02i", $hr, $min, $sec))
    If $seconds <= 0 Then
        AdlibUnRegister("Countdown")
        MsgBox(0, "Information", "Countdown reached 00:00:00")
        Exit
    EndIf
    $seconds -= 1
EndFunc

or

;coded by UEZ 2011
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global Const $hGUI = GUICreate("Simple Countdown by UEZ 2011", 833, 179)
GUISetBkColor(0xBFCDDB)
Global Const $FontQual = 4
Global Const $idInputStd = GUICtrlCreateInput("1", 8, 8, 209, 159, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY, $ES_NUMBER))
GUICtrlSetFont(-1, 100, 400, 0, "Times New Roman", $FontQual)
Global Const $idUpdownStd = GUICtrlCreateUpdown($idInputStd)
GUICtrlSetLimit(-1, 999, 0)
Global Const $idLableDP1 = GUICtrlCreateLabel(":", 226, 0, 41, 155)
GUICtrlSetFont(-1, 100, 400, 0, "Times New Roman", $FontQual)
Global Const $idInputMin = GUICtrlCreateInput("0", 277, 8, 153, 159, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY, $ES_NUMBER))
GUICtrlSetFont(-1, 100, 400, 0, "Times New Roman", $FontQual)
Global Const $idUpdownMin = GUICtrlCreateUpdown($idInputMin)
GUICtrlSetLimit(-1, 59, 0)
Global Const $idLableDP2 = GUICtrlCreateLabel(":", 440, 0, 41, 155)
GUICtrlSetFont(-1, 100, 400, 0, "Times New Roman", $FontQual)
Global Const $idInputSek = GUICtrlCreateInput("0", 489, 8, 153, 159, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY, $ES_NUMBER))
GUICtrlSetFont(-1, 100, 400, 0, "Times New Roman", $FontQual)
Global Const $idUpdownSek = GUICtrlCreateUpdown($idInputSek)
GUICtrlSetLimit(-1, 59, 0)
Global Const $idButtonStart = GUICtrlCreateButton("Start", 672, 8, 147, 113)
GUICtrlSetFont(-1, 50, 400, 0, "Times New Roman", $FontQual)
Global Const $idButtonStop = GUICtrlCreateButton("Stop", 672, 136, 147, 33)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW)
ControlFocus("", "", $idButtonStart)

Global $nMsg, $seconds

While Sleep(30)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idButtonStart
            $seconds = GUICtrlRead($idInputStd) * 60 ^ 2 + GUICtrlRead($idInputMin) * 60 + GUICtrlRead($idInputSek) - 1
            If $seconds > 0 Then
                GUICtrlSetState($idButtonStart, $GUI_DISABLE)
                GUICtrlSetState($idUpdownStd, $GUI_DISABLE)
                GUICtrlSetState($idUpdownMin, $GUI_DISABLE)
                GUICtrlSetState($idUpdownSek, $GUI_DISABLE)
                GUICtrlSetState($idButtonStop, $GUI_ENABLE)
                AdlibRegister("Countdown", 1000)
            EndIf
        Case $idButtonStop
            AdlibUnRegister("Countdown")
            GUICtrlSetState($idButtonStart, $GUI_ENABLE)
            GUICtrlSetState($idUpdownStd, $GUI_ENABLE)
            GUICtrlSetState($idUpdownMin, $GUI_ENABLE)
            GUICtrlSetState($idUpdownSek, $GUI_ENABLE)
            GUICtrlSetState($idButtonStop, $GUI_DISABLE)
    EndSwitch
WEnd

Func Countdown()
    Local $sec, $min, $hr
    $sec = Mod($seconds, 60)
    $min = Mod($seconds / 60, 60)
    $hr = Floor($seconds / 60 ^ 2)
    If GUICtrlRead($idInputStd) <> $hr Then GUICtrlSetData($idInputStd, StringFormat("%01i", $hr))
    If GUICtrlRead($idInputMin) <> $min Then GUICtrlSetData($idInputMin, StringFormat("%01i", $min))
    GUICtrlSetData($idInputSek, StringFormat("%01i", $sec))
    If $seconds <= 0 Then
        AdlibUnRegister("Countdown")
        MsgBox(0, "Information", "Countdown reached 00:00:00")
        Exit
    EndIf
    $seconds -= 1
EndFunc   ;==>Countdown

I hope it helps you.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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