Jump to content

Countdown Shutdown Timer


Recommended Posts

I made this script to choose the time in the updown boxes before the computer shutsdown, reboots, etc. But it does the action immediately. What am I doing wrong?

#NoTrayIcon
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <UpdownConstants.au3>

Global $Time, $Paused

HotKeySet("{PAUSE}", "TogglePause")

$Form2 = GUICreate("Shutdown Menu", 413, 179, 192, 123)
$Input1 = GUICtrlCreateInput("0", 76, 128, 73, 21)
$Updown1 = GUICtrlCreateUpdown($Input1, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 168, 0)
$Label1 = GUICtrlCreateLabel("Seconds:", 264, 104, 49, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput("0", 168, 128, 73, 21)
$Updown2 = GUICtrlCreateUpdown($Input2, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 59, 0)
$Label2 = GUICtrlCreateLabel("Minutes:", 168, 104, 44, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Input3 = GUICtrlCreateInput("0", 264, 128, 73, 21)
$Updown3 = GUICtrlCreateUpdown($Input3, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 59, 0)
$Label3 = GUICtrlCreateLabel("Hours:", 74, 104, 35, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Hibernate", 16, 40, 89, 33, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Stand By", 112, 40, 89, 33, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Shutdown", 208, 40, 89, 33, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Restart", 304, 40, 89, 33, $WS_GROUP)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Sleep($Time)
            Run(@ComSpec & " /c " & 'powercfg -h on', "", @SW_HIDE)
            Shutdown(64)
        Case $Button2
            Sleep($Time)
            Shutdown(32)
        Case $Button3
            Sleep($Time)
            Shutdown(13)
        Case $Button4
            Sleep($Time)
            Shutdown(6)
    EndSwitch
WEnd

Func Timer()
    $Sec = GUICtrlRead($Input1)
    $Min = GUICtrlRead($Input2)
    $Hour = GUICtrlRead($Input3)
    $Time = $Sec + $Min * 60 + $Hour * 3600
    $Time = $Time * 100
EndFunc   ;==>Timer

Func TogglePause()
    $Paused = Not $Paused
    While $Paused
        Sleep(10000)
        ToolTip('Script is "Paused"', 0, 0)
    WEnd
    ToolTip("")
EndFunc   ;==>TogglePause
Link to comment
Share on other sites

Try this:

#NoTrayIcon
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <UpdownConstants.au3>

Global $Time, $Paused = 1, $sum, $display = 1, $shutdown = False

HotKeySet("{PAUSE}", "TogglePause")

$Form2 = GUICreate("Shutdown Menu", 413, 179, 192, 123)
$Input1 = GUICtrlCreateInput("0", 76, 128, 73, 21)
$Updown1 = GUICtrlCreateUpdown($Input1, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 168, 0)
$Label1 = GUICtrlCreateLabel("Seconds:", 264, 104, 49, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput("0", 168, 128, 73, 21)
$Updown2 = GUICtrlCreateUpdown($Input2, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 59, 0)
$Label2 = GUICtrlCreateLabel("Minutes:", 168, 104, 44, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Input3 = GUICtrlCreateInput("0", 264, 128, 73, 21)
$Updown3 = GUICtrlCreateUpdown($Input3, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 59, 0)
$Label3 = GUICtrlCreateLabel("Hours:", 74, 104, 35, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Hibernate", 16, 40, 89, 33, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Stand By", 112, 40, 89, 33, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Shutdown", 208, 40, 89, 33, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Restart", 304, 40, 89, 33, $WS_GROUP)
$Button5 = GUICtrlCreateButton("Abort", 350, 122, 49, 33, $WS_GROUP)
GUICtrlSetState($Button5, $GUI_DISABLE)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Countdown(64)
        Case $Button2
            Countdown(32)
        Case $Button3
            Countdown(13)
        Case $Button4
            Countdown(6)
    EndSwitch
    If $Paused = -1 And $display Then
        ToolTip('Script is "Paused"', 0, 0)
        $display = 0
    EndIf
    If $Paused = 1 Then ToolTip("")

WEnd

Func Countdown($sd_code)
    _Disable_Buttons()
    Local $nMsg
    Local $abort = False
    $Sec = GUICtrlRead($Input3)
    $Min = GUICtrlRead($Input2)
    $Hour = GUICtrlRead($Input1)
    $sum = $Sec + 60 * $Min + 3600 * $Hour
    AdlibRegister("Counter", 1000)
    While Not $shutdown
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $Button5
                $abort = True
                AdlibUnRegister("Counter")
                _Enable_Buttons()
                ExitLoop
        EndSwitch
    WEnd
    If Not $abort Then
        ConsoleWrite("Executing shutdown function!" & @LF)
;~      If $sd_code = 64 Then Run(@ComSpec & " /c " & 'powercfg -h on', "", @SW_HIDE)
;~      Shutdown($sd_code)
    EndIf
EndFunc   ;==>Timer

Func Counter()
    Local $s, $m, $Hour
    If $sum = 0 Then
        AdlibUnRegister("Counter")
        $display = 1
        $shutdown = True
    Else
        $sum -= 1
        $s = Mod($sum, 60)
        $m = Mod(Int($sum / 60), 60)
        $h = Int($sum / 60 ^ 2)
        GUICtrlSetData($Input3, $s)
        GUICtrlSetData($Input2, $m)
        GUICtrlSetData($Input1, $h)
    EndIf
EndFunc


Func TogglePause()
    $Paused *= -1
    If $Paused -1 Then
        AdlibUnRegister("Counter")
    Else
        AdlibRegister("Counter", 1000)
    EndIf
EndFunc   ;==>TogglePause

Func _Disable_Buttons()
    GUICtrlSetState($Button1, $GUI_DISABLE)
    GUICtrlSetState($Button2, $GUI_DISABLE)
    GUICtrlSetState($Button3, $GUI_DISABLE)
    GUICtrlSetState($Button4, $GUI_DISABLE)
    GUICtrlSetState($Button5, $GUI_ENABLE)
EndFunc

Func _Enable_Buttons()
    GUICtrlSetState($Button1, $GUI_ENABLE)
    GUICtrlSetState($Button2, $GUI_ENABLE)
    GUICtrlSetState($Button3, $GUI_ENABLE)
    GUICtrlSetState($Button4, $GUI_ENABLE)
    GUICtrlSetState($Button5, $GUI_DISABLE)
EndFunc

I would add also an "Abort" button! Done.

Br,

UEZ

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

Try this:

#NoTrayIcon
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <UpdownConstants.au3>

Global $Time, $Paused = 1, $sum, $display = 1

HotKeySet("{PAUSE}", "TogglePause")

$Form2 = GUICreate("Shutdown Menu", 413, 179, 192, 123)
$Input1 = GUICtrlCreateInput("0", 76, 128, 73, 21)
$Updown1 = GUICtrlCreateUpdown($Input1, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 168, 0)
$Label1 = GUICtrlCreateLabel("Seconds:", 264, 104, 49, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput("0", 168, 128, 73, 21)
$Updown2 = GUICtrlCreateUpdown($Input2, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 59, 0)
$Label2 = GUICtrlCreateLabel("Minutes:", 168, 104, 44, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Input3 = GUICtrlCreateInput("0", 264, 128, 73, 21)
$Updown3 = GUICtrlCreateUpdown($Input3, BitOR($UDS_ALIGNRIGHT, $UDS_ARROWKEYS, $UDS_NOTHOUSANDS))
GUICtrlSetLimit(-1, 59, 0)
$Label3 = GUICtrlCreateLabel("Hours:", 74, 104, 35, 17)
GUICtrlSetFont(-1, 9, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Hibernate", 16, 40, 89, 33, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Stand By", 112, 40, 89, 33, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Shutdown", 208, 40, 89, 33, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Restart", 304, 40, 89, 33, $WS_GROUP)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Countdown()
;~             Run(@ComSpec & " /c " & 'powercfg -h on', "", @SW_HIDE)
;~             Shutdown(64)
        Case $Button2
            Countdown()
;~             Shutdown(32)
        Case $Button3
            Countdown()
;~             Shutdown(13)
        Case $Button4
            Countdown()
;~             Shutdown(6)
    EndSwitch
    If $Paused = -1 And $display Then
        ToolTip('Script is "Paused"', 0, 0)
        $display = 0
    EndIf
    If $Paused = 1 Then ToolTip("")

WEnd

Func Countdown()
    $Sec = GUICtrlRead($Input3)
    $Min = GUICtrlRead($Input2)
    $Hour = GUICtrlRead($Input1)
    $sum = $Sec + 60 * $Min + 3600 * $Hour
    AdlibRegister("Counter", 1000)
EndFunc   ;==>Timer

Func Counter()
    Local $s, $m, $Hour
    If $sum = 0 Then
        AdlibUnRegister("Counter")
        $display = 1
    Else
        $sum -= 1
        $s = Mod($sum, 60)
        $m = Mod(Int($sum / 60), 60)
        $h = Int($sum / 60 ^ 2)
        GUICtrlSetData($Input3, $s)
        GUICtrlSetData($Input2, $m)
        GUICtrlSetData($Input1, $h)
    EndIf
EndFunc


Func TogglePause()
    $Paused *= -1
    If $Paused -1 Then
        AdlibUnRegister("Counter")
    Else
        AdlibRegister("Counter", 1000)
    EndIf
EndFunc   ;==>TogglePause

I would add also an "Abort" button!

Br,

UEZ

When I click any button it immediately does that action. Then it starts counting down, if I chose standby or hibernate. I want it to do the countdown and then when it reaches zero, then do the action.
Link to comment
Share on other sites

Yes, that was the reason why edited the code in the same time when you replied! :)

Check the updated code.

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