Sign in to follow this  
Followers 0
PlayZoneUK

Restart Script (Count down timer with Progress Bar)

5 posts in this topic

#1 ·  Posted (edited)

Seasonal Greetings,

I wonder if someone could help me with a little restart script Ive been working on. In a nut shell the GUI part of the script has already been created (see below) however Ive run into a brick wall with the Progress bar, as the time decreases I want the progress bar to increase and Im unsure how to do this.

Thanks in advance

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Timers.au3>
#include <Date.au3>
#include <ProgressConstants.au3>

Global $iCountdown = 30 ; Sets the time-out in seconds

$Form1 = GUICreate("Restart Required", 415, 145, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS), $WS_EX_WINDOWEDGE) ; Creates GUI Window
$Progress = GUICtrlCreateProgress(10, 65, 395, 15, $PBS_SMOOTH) ; Creates Progress Bar
GUICtrlSetColor($Progress, 0xff0000) ; Sets Progress Bar Colour
$RestartNow = GUICtrlCreateButton("Restart Now", 235, 112, 80, 23, 0) ; Creates Restart Now Button
$RestartLater = GUICtrlCreateButton("Restart Later", 325, 112, 80, 23, 0) ; Creates Restart Latter Button
$Label1 = GUICtrlCreateLabel("To help improve performance, it is recommended that this workstation is restarted. Windows will restart your computer automatically in " & _SecsToTime($iCountdown) & " minutes.", 10, 10, 405, 25)
$Label2 = GUICtrlCreateLabel("Do you want to restart your computer now?", 10, 90, 405, 20)
GUISetState(@SW_SHOW)

_Timer_SetTimer($Form1, 1000, '_Countdown')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $RestartNow
            Exit ;Exit for now until progress bar is sorted
        Case $RestartLater
            Exit
    EndSwitch
WEnd

Func _Countdown($hWnd, $iMsg, $iIDTimer, $dwTime)
    $iCountdown -= 1
    If $iCountdown > 0 Then
        GUICtrlSetData($Label1, "To help improve performance, it is recommended that this workstation is restarted. Windows will restart your computer automatically in " & _SecsToTime($iCountdown) & " minutes.")
    ElseIf $iCountdown = 0 Then
        _Timer_KillTimer($hWnd, $iIDTimer)
        ControlClick($Form1, '', $RestartNow)
    EndIf
EndFunc  ;==>_Countdown

Func _SecsToTime($iSecs)
    Local $iHours, $iMins, $iSec_s
    _TicksToTime($iSecs*1000,$iHours,$iMins,$iSec_s)
    Return StringFormat("%02i:%02i",$iMins, $iSec_s)
EndFunc
Edited by PlayZoneUK

[u]Words of Wisdom and Favourite Quotes:[/u]'Nothing is impossible, despite what other people say and think, prove them wrong and show them what you can do!''Understanding is a three edged sword, your side, their side and the truth!''Nothing, I shoot ever gets back up again!''The truth that humanity has never been able to grasp, is that death may be the only absolute freedom there is!''The answer's no, now what's the question?''I hear what you're saying, but we're gonna do it my way!''You don't keep me informed' > 'But I sent you an e-Mail regarding this issue and you read it!' > 'oooh you can't expect an e-Mail to register!''How are you today?' > 'Fair to suicidal!'

Share this post


Link to post
Share on other sites



CODE
$percent_value = Round ($time_elapsed / $total time, 2)

GuiCtrlSetData ($Progress, $percent_value)

Share this post


Link to post
Share on other sites

clarinetmeister's suggestion doesn't quite work with this code. I'd recommend replacing the calculation with:

$percent_value = Ceiling(($time_elapsed / $total time) * 100)

And then just for kicks throw "GUICtrlSetData($Progress, 100)" right before or after your setting of the progress bar color.

PlayZoneUK, I tried this code out and thanks for posting it. I was looking to do the same thing as you but hadn't found the _Timer_SetTimer function yet (pretty new to AutoIt still).

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Glad I could help and thanks for pointing in the right direction...

Hey I'm still kinda new to AutoIT Script myself... although I've been dipping in and out since 2004 :)

This script is intended for a hospital environment, you'd be surprise how many consultants blame the equipment especially when its been up for 66+ days (come on people this is Windows its needs a restart once a day at the most!)

Naturally those same consultants would have me in stocks and pelted with tomatoes if the script executes and restarts while they are in the middle of looking at X-RAYS (Even if I give them a timeout of 5 minutes). Therefore Ive modified the original script so it reads the configuration from an external ini file i.e. DelayTime and Process; this makes the script customisable even when compiled. If there are any processes specified (e.g. notepad.exe or mspaint.exe) the script will not take any action and just exit.

Any way here is the end result... hope this helps

Enforcer.au3

#NoTrayIcon
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Timers.au3>
#include <Date.au3>
#include <ProgressConstants.au3>

FileInstall("Enforcer.ini", @ScriptDir & "\Enforcer.ini")

Global  $Processes = IniRead(@ScriptDir & "\Enforcer.ini", "Parameters", "Process", "") ; Reads from config file, a list of process.
$Cancel_If_Running = StringSplit( $Processes, "|") ; Takes the $Processes value into an Array

If IsArray ($Cancel_If_Running) Then
    For $i = 1 to $Cancel_If_Running[0]
        If ProcessExists ($Cancel_If_Running[$i]) Then Exit ; If specified process(es) is / are running the script will exit.
    Next
EndIf

Global $iCountdown = IniRead(@ScriptDir & "\Enforcer.ini", "Parameters", "DelayTime", 10) ; Sets the time-out in seconds
Global $Prompt = "To help improve performance, it is recommended that this workstation is restarted." & @CRLF & "Windows will restart your computer automatically in "
Global $iTotal_Time = $iCountdown ; Copies the starting time to another variable.

$Form1 = GUICreate("Restart Required", 415, 145, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS), $WS_EX_WINDOWEDGE) ; Creates GUI Window
$Progress = GUICtrlCreateProgress(10, 65, 395, 15, $PBS_SMOOTH) ; Creates Progress Bar
GUICtrlSetColor($Progress, 0xff0000) ; Sets Progress Bar Colour
$RestartNow = GUICtrlCreateButton("Restart Now", 235, 112, 80, 23, 0) ; Creates Restart Now Button
$RestartLater = GUICtrlCreateButton("Restart Later", 325, 112, 80, 23, 0) ; Creates Restart Latter Button
$Label1 = GUICtrlCreateLabel($Prompt & _SecsToTime($iCountdown) & " minutes.", 10, 10, 405, 55)
$Label2 = GUICtrlCreateLabel("Do you want to restart your computer now?", 10, 90, 405, 20)
GUISetState(@SW_SHOW)

_Timer_SetTimer($Form1, 1000, '_Countdown')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $RestartNow
            _Restart()
        Case $RestartLater
            Exit
    EndSwitch
WEnd

Func _Countdown($hWnd, $iMsg, $iIDTimer, $dwTime)
    $iCountdown -= 1
    $percent_value = Floor(($iCountdown / $iTotal_Time) * 100)
    $percent_value = 100 - $percent_value
    If $iCountdown > 0 Then
        GUICtrlSetData($Label1, $Prompt & _SecsToTime($iCountdown) & " minutes.")
        GUICtrlSetData($Progress, $percent_value)
    ElseIf $iCountdown = 0 Then
        GUICtrlSetData($Label1, $Prompt & _SecsToTime($iCountdown) & " minutes.")
        GUICtrlSetData($Progress, $percent_value)
        _Timer_KillTimer($hWnd, $iIDTimer)
        ControlClick($Form1, '', $RestartNow) ; Default action when Countdown equals 0
    EndIf
EndFunc  ;==>_Countdown

Func _SecsToTime($iSecs)
    Local $iHours, $iMins, $iSec_s
    _TicksToTime($iSecs*1000,$iHours,$iMins,$iSec_s)
    Return StringFormat("%02i:%02i",$iMins, $iSec_s)
EndFunc

Func _Restart()
    Shutdown(6)  ;Force a reboot
    Exit
EndFunc

Enforcer.ini

CODE
[Parameters]

DelayTime=300

Process=notepad.exe

[Configuration]

DelayTime = Time before restart, value in seconds e.g. 5 minutes is 300 seconds.

Process = If the specified process(es) are running then take no action and exit e.g. notepad.exe|mspaint.exe

To ensure that the workstation does restart even when the computer is locked I used the following command to create a scheduled task which will run as system:

at 03:00 /INTERACTIVE /EVERY:M,T,W,Th,F,S,Su "%PROGRAMFILES%\Enforcer\Enforcer.exe"

Edited by PlayZoneUK

[u]Words of Wisdom and Favourite Quotes:[/u]'Nothing is impossible, despite what other people say and think, prove them wrong and show them what you can do!''Understanding is a three edged sword, your side, their side and the truth!''Nothing, I shoot ever gets back up again!''The truth that humanity has never been able to grasp, is that death may be the only absolute freedom there is!''The answer's no, now what's the question?''I hear what you're saying, but we're gonna do it my way!''You don't keep me informed' > 'But I sent you an e-Mail regarding this issue and you read it!' > 'oooh you can't expect an e-Mail to register!''How are you today?' > 'Fair to suicidal!'

Share this post


Link to post
Share on other sites

Funny you should say so. I'm also finishing up a script with much of the same functionality as yours for use at a hospital for all the same reasons. I'll post it when it's complete. though parts will be identical thanks to your original scripting.

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  
Followers 0