Thursday, October 11, 2007

AutoIT Timer/Countdown

I have started using AutoIT v3 to write automated installers and other scripted windows programming lately. When I send out an update to a client computer, sometimes I want to be able to notify the user and let them delay the update for a set amount of time. Using the TimerInit() & TimerDiff() functions I am able to pop up a message and begin a timer.

#include <GUIConstants.au3>
;
;milliseconds for timer, start the timer and label text
;
$ms = 3601000
$timer = TimerInit()
$labeltext = "The IT department needs to install the latest approved updates..."
;
;gui window with text, Install Now button and timer window
;
GUICreate("Software Update", 350, 330, -1, -1, $WS_DLGFRAME, $WS_EX_TOPMOST)
$Install = GUICtrlCreateButton ("Install Now", 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) and (GUIGetMsg() <> $Install)
;
$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)
;
GUICtrlSetData($Input, $time)
;
WEnd
;
Run("yourcommand.exe")

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Seems to be a couple of include statements missing:
    WindowsConstants.au3
    StaticConstants.au3
    EditConstants.au3

    Not sure if anything else is missing, but those were needed to run the script without errors.

    Anyway, handy script. Thanks.

    ReplyDelete
  4. Thanks Sanjoval. Looks to be required in newer versions. Thanks for posting!

    ReplyDelete