Jump to content

Shutdown tool


Recommended Posts

A few weeks ago I decided to start making a Shutdown tool, since I was backing some of my files onto my external HDD and it took a lot of time. It was late at night, and I didn't want to wait up to turn off the computer. Last night I again took a backup, and I thought about my script, and started working on it again. I made some improvements from the script before, but the countdown is still not working.

Let me show you the script first...

#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "end")
HotKeySet("{F10}", "Start")

#Region ### START Koda GUI section ### Form=C:\Users\Jan\Documents\koda_1.7.2.2_b204_2010-02-04\Forms\Shutdown tool.kxf
$Form1_1 = GUICreate("Shutdown tool", 218, 105, 192, 124)
$Input1 = GUICtrlCreateInput("00", 40, 64, 25, 21)
$Input2 = GUICtrlCreateInput("10", 96, 64, 25, 21)
$Input3 = GUICtrlCreateInput("30", 152, 64, 25, 21)
$Label1 = GUICtrlCreateLabel("Hour(s)", 40, 48, 38, 17)
$Label2 = GUICtrlCreateLabel("Minute(s)", 88, 48, 47, 17)
$Label3 = GUICtrlCreateLabel("Second(s)", 144, 48, 52, 17)
$Combo1 = GUICtrlCreateCombo("Shutdown in", 40, 16, 145, 25)
GUICtrlSetData(-1, "Shutdown at")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func Start()
If GUICtrlRead($Combo1) = "Shutdown in" Then
    countdown()
Else
    shutdownat()
EndIf
EndFunc

Func countdown()
    $hour = 1000 * 60 * 60
    $min = 1000 *60
    $sec = 1000

    ;Dim $Total = GUICtrlRead($input1) * $hour + GUICtrlRead($input2) * $min + GUICtrlRead($input3) * $sec   ;I thought it had to be like this, but because it already sleep($sec) each time, then you don't have to * 1000 in the equation or you get a horribly wrong number...
    
        Dim $Total = GUICtrlRead($input1) * 60 * 60 + GUICtrlRead($input2) * 60 + GUICtrlRead($input3)

    For $s = $Total To 0 Step -1        ; Seconds left
        Sleep($sec)
        $TT = ToolTip("System will shutdown in " & $s & " seconds!", 50, 50, "Countdown until shutdown", 1)
    Next

    Sleep($Total)
    Dim $TT = ToolTip("")

    MsgBox(0, "Shutdown", "Your system is shutting down.", 5 * $sec)

    Exit

    ;For $h = GUICtrlRead($input1) To 0 Step -1       ; Timer for hours
    ;   Sleep(1000)
        ;Sleep($hour)                           ;
    ;   GUICtrlSetData($input1, $h)
    ;Next       ;here ends the timer

    ;For $m = GUICtrlRead($input2) To 0 Step -1     ; Timer for minutes
    ;   Sleep($min)
    ;   GUICtrlSetData($input2, $m)
    ;Next
    ;
EndFunc

Func shutdownat()
    For $s = GUICtrlRead($input3) To 0 Step -1
        Sleep(1000)
        Tooltip($s, 50, 50, "Countdown", 1)
    Next

If @HOUR = GUICtrlRead($input1) And @MIN = GUICtrlRead($input2) Then
            MsgBox(1, "Shutdown", "Shutdown would happen now!")
        EndIf
EndFunc

Func end()
    Exit
EndFunc

(I've only put MsgBoxes when the script should shutdown the computer, but the shutdown function will be added later)

Now my problem lies in the countdown. Right now it will show how many seconds there is until shutdown e.g. 26234 seconds. I'd like it to show

02 hours, 32 minutes and 05 seconds until shutdown.
And each time seconds get till 0, I'd like for it to start from 59 again if there still is something more than 00 in the minutes. And the same process with the minutes if there still is something in the hours.

Maybe I'm missing a really easy way to fix this, but I've been struggling with it for a few hours now...

shutdown.au3

Link to comment
Share on other sites

This is a snippet from a script I wrote once... I think this is something to do with what you are asking... it's basic arithmetic... it should be obvious how to get years, months, weeks, days as well... If you need double digits e.g. 00 minutes convert numbers to strings and then do a <10 test and add a leading zero if true...

$Tseconds=$RemainingSecs
    $hours = int($Tseconds/3600)
    $Remsecs = $Tseconds - ($hours * 3600)
    $minutes = int($Remsecs / 60)
    $Seconds = $Remsecs - ($minutes * 60)
    $Remainingshow = $hours & " hours, " & $minutes & " minutes and " & $Seconds & " seconds"
Edited by contrex
Link to comment
Share on other sites

Thanks for the fast reply, and I do understand how it works. Out of the Total number of seconds it finds out how many hours that is, and how many minutes it is (without any decimals).

I can't get it to update the tooltip other than the way that I did. (e.g. 75..74..73 and not 01 minute(s) and 15..14..13 seconds)

This is the countdown function, as it is now..

Func countdown()
    $hour = 1000 * 60 * 60
    $min = 1000 *60
    $sec = 1000


    Dim $Total = GUICtrlRead($input1) * 60 * 60 + GUICtrlRead($input2) * 60 + GUICtrlRead($input3)


    $Tseconds=$Total
    $hours = int($Tseconds/3600)
    $Remsecs = $Tseconds - ($hours * 3600)
    $minutes = int($Remsecs / 60)
    $Seconds = $Remsecs - ($minutes * 60)


    For $Seconds = $Total To 0 Step -1      ; Tooltip Countdown
        Sleep($sec)
        $TT = ToolTip("System will shutdown in " & $hours & "hours, " & $minutes & " minutes and " & $Seconds & " seconds." & @CRLF & "To cancel shutdown and exit the script press the ESCAPE key.", 50, 50, "Countdown until shutdown", 1)
    Next

    Sleep($Total)
    Dim $TT = ToolTip("")

    MsgBox(0, "Shutdown", "Your system is shutting down.", 5 * $sec)

    Exit

    ;For $h = GUICtrlRead($input1) To 0 Step -1       ; Timer for hours
    ;   Sleep(1000)
        ;Sleep($hour)                           ;
    ;   GUICtrlSetData($input1, $h)
    ;Next       ;here ends the timer

    ;For $m = GUICtrlRead($input2) To 0 Step -1     ; Timer for minutes
    ;   Sleep($min)
    ;   GUICtrlSetData($input2, $m)
    ;Next
    ;
EndFunc

If I put into the GUI 00 hours, 1 minute and 15 seconds it shows me the following in my ToolTip: "System will shutdown in 0 hours, 0 minutes and 75 seconds."

and then it counts down from 75 to 0. :blink:

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