Jump to content

Countdown Timer not properly counting


Recommended Posts

:blink: Hello my fello Autoit gurus

I recently snagged some code to help with a project that creates a countdown timer and the executes a program. I notice however the program does not count accurate seconds on every machine. I have run this from a network installation as well as locally. Does anyone have any ideas as to why this is happening? Is it possible to fix?

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
;milliseconds for timer, start the timer and label text
$ms = 10000 ;3601000
$timer = TimerInit()
Local $iRun = True
$labeltext = "The  IT department is applying updates to your computer..."
;
;gui window with text, Install Now button and timer window
GUICreate("Software Updates", 350, 330, -1, -1, $WS_DLGFRAME, $WS_EX_TOPMOST)
$Cancel = GUICtrlCreateButton ("Cancel", 100, 250, 150, 50)
$Label = GUICtrlCreateLabel($labeltext, 10, 10, 330, 200)
$Input = GUICtrlCreateInput("", 140, 225, 75, 20, BitOr($SS_CENTER, $ES_READONLY))
GUISetState (@SW_SHOW)
;
;wait for timer to expire or for Install Now button to be clicked
;each time around the loop, it will update the timer displayed on the screen
;
While (TimerDiff($timer) < $ms)
;
$seconds = TimerDiff($timer)/1000
$diff = $seconds - ($ms/1000)
;
$minutes = Int($diff / 60)
$secondsRem = $diff - ($minutes * 60)
;
$minutes = $minutes * -1
$secondsRem = $secondsRem * -1
$time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
 If GUIGetMsg() = $Cancel then
    $iRun = False
    ExitLoop
 EndIf
GUICtrlSetData($Input, $time)
;

WEnd
If $iRun Then Run("calc.exe")
Link to comment
Share on other sites

  • Developers

I notice however the program does not count accurate seconds on every machine.

What exactly do you mean with this statement? what exactly happens on PCs were it goes wrong?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Maybe, your computer is prioritizing CPU usage to other processes that may be happening and the While - WEnd loop in the AutoIt script is missing out thus increasing each individual loop to more than a second in duration.

I would like to know if this script will accurately count down the seconds on every machine.

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <Timers.au3>
; http://www.autoitscript.com/forum/index.php?showtopic=117781&view=findpost&p=820261
Global $Input, $ms = 10 ; seconds

Local $iRun = True
Local $labeltext = "The IT department is applying updates to your computer..."
Local $iTimer, $hGUI, $Cancel, $Label, $Input, $minutes = Int($ms / 60)

$hGUI = GUICreate("Software Updates", 350, 330, -1, -1, $WS_DLGFRAME, $WS_EX_TOPMOST)
$Cancel = GUICtrlCreateButton("Cancel", 100, 250, 150, 50)
$Label = GUICtrlCreateLabel($labeltext, 10, 10, 330, 200)
$Input = GUICtrlCreateInput(StringFormat("%02d:%02d", $minutes, $ms - ($minutes * 60)), 140, 225, 75, 20, BitOR($SS_CENTER, $ES_READONLY))
GUISetState(@SW_SHOW)

$iTimer = _Timer_SetTimer($hGUI, 1000, "_CDD")
;
;wait for timer to expire or for Cancel button to be clicked
;each second, the timer will update & displayed on the window.
;
While 1
    Select
        Case GUIGetMsg() = $Cancel
            $iRun = False
            ExitLoop
        Case $ms <= 0
            ExitLoop
    EndSelect
WEnd

_Timer_KillTimer($hGUI, $iTimer)

If $iRun Then Run("calc.exe")


; CountDownDisplay
Func _CDD($hWnd, $Msg, $iIDTimer, $dwTime)
    $ms -= 1
    Local $minutes = Int($ms / 60)
    GUICtrlSetData($Input, StringFormat("%02d:%02d", $minutes, $ms - ($minutes * 60)))
EndFunc ;==>_CDD

And note the use of the single StringFormat() function using two variables.

Edit: Added _Timer_KillTimer().

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