Jump to content

Button press while in loop


AutoMT
 Share

Recommended Posts

Hi, I'm writing a script that is basically a front end for the "shutdown" command in DOS. It'll be used to remotely reboot computers that are on a network. It has a reboot button and an abort button in the GUI, but the abort will not work while a "countdown" loop is running in the btnRebootClick function (thus defeating the purpose of an abort button :) )

#include <GUIConstants.au3>
#include <Process.au3>
$MachineToReboot = 0

Opt("GUIOnEventMode", 1)
$frmMain = GUICreate("Remote Reboot", 349, 145, 191, 142)
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseXClick")
$inpMachineName = GUICtrlCreateInput("", 168, 16, 105, 21)
$Label1 = GUICtrlCreateLabel("Machine Name:", 16, 16, 79, 17)
$Label2 = GUICtrlCreateLabel("Shutdown Time (seconds):", 16, 48, 130, 17)
$inpShutdownTime = GUICtrlCreateInput("30", 168, 48, 57, 21)
$lblCountDown = GUICtrlCreateLabel("", 16, 80, 260, 52, $SS_SUNKEN)
$btnReboot = GUICtrlCreateButton("Reboot", 288, 16, 49, 49, 0)
GUICtrlSetOnEvent(-1, "btnRebootClick")
$btnAbort = GUICtrlCreateButton("ABORT", 288, 80, 49, 49, 0)
GUICtrlSetOnEvent(-1, "btnAbortClick")
GUICtrlSetState($btnAbort, $GUI_DISABLE)
GUISetState(@SW_SHOW)


While 1
Sleep(100)
WEnd

Func CloseXClick()
    Exit
EndFunc

Func btnRebootClick()
    $MachineToReboot = GUICtrlRead($inpMachineName)
    $RebootTime = GUICtrlRead($inpShutdownTime)
    GUICtrlSetData($lblCountdown, "Pinging " &$MachineToReboot & "...")
    $Pinged = Ping($MachineToReboot, 1000)
    If Not StringIsDigit($RebootTime) Then
        GUICtrlSetData($lblCountdown, "Shutdown time must be in digits.")
        ControlFocus("Remote Reboot", "", $inpShutDownTime)
        Send("+{HOME}")
    Else
    If Not $Pinged Then
        GUICtrlSetData($lblCountdown, "Cannot ping " & $MachineToReboot & ".  Check machine name and try again.")
        ControlFocus("Remote Reboot", "", $inpMachineName)
        Send("+{HOME}")
    Else
        GUICtrlSetState($btnAbort, $GUI_ENABLE)
        GUICtrlSetState($btnReboot, $GUI_DISABLE)
        ControlFocus("Remote Reboot", "", $btnAbort)
        GUICtrlSetData($lblCountdown, $MachineToReboot & " pinged.  Rebooting...")
        Sleep(500)
        _RunDOS ("shutdown -m \\" & $MachineToReboot & " -r -t " & $RebootTime)
        $Count = $RebootTime
        ;==================================================================================================
        While $Count > 0
            GUICtrlSetData($lblCountdown, "Rebooting " & $MachineToReboot & " in " & $Count & " seconds...")
            Sleep(1000)
            $Count = $Count - 1
        WEnd
        ;==================================================================================================
    EndIf
    EndIf
EndFunc

Func btnAbortClick()
    GUICtrlSetData($lblCountdown, "Aborting...")
    _RunDOS ("shutdown -m \\" & $MachineToReboot & " -a")
    GUICtrlSetData($lblCountdown, "Reboot of " & $MachineToReboot & " aborted.")
    GUICtrlSetState($btnAbort, $GUI_DISABLE)
    GUICtrlSetState($btnReboot, $GUI_ENABLE)
    ControlFocus("Remote Reboot", "", $inpMachineName)
    Send("+{HOME}")
EndFunc

Couldn't find anything in searches. Sorry about my dirty coding.

Also, is there a way to make it so a user can hit enter while an inputbox has the focus to begin a function instead of using one of the buttons?

Edited by AutoMT
Link to comment
Share on other sites

#include <GUIConstants.au3>
#include <Process.au3>
$MachineToReboot = 0
$count = 0
Opt("GUIOnEventMode", 1)

$frmMain = GUICreate("Remote Reboot", 349, 145, 191, 142)
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseXClick")
$inpMachineName = GUICtrlCreateInput("", 168, 16, 105, 21)
$Label1 = GUICtrlCreateLabel("Machine Name:", 16, 16, 79, 17)
$Label2 = GUICtrlCreateLabel("Shutdown Time (seconds):", 16, 48, 130, 17)
$inpShutdownTime = GUICtrlCreateInput("30", 168, 48, 57, 21)
$lblCountDown = GUICtrlCreateLabel("", 16, 80, 260, 52, $SS_SUNKEN)
$btnReboot = GUICtrlCreateButton("Reboot", 288, 16, 49, 49, 0)
GUICtrlSetOnEvent(-1, "btnRebootClick")
$btnAbort = GUICtrlCreateButton("ABORT", 288, 80, 49, 49, 0)
GUICtrlSetOnEvent(-1, "btnAbortClick")
GUICtrlSetState($btnAbort, $GUI_DISABLE)
GUISetState(@SW_SHOW)


While 1
    Sleep(100)
WEnd

Func CloseXClick()
    Exit
EndFunc

Func btnRebootClick()
    $MachineToReboot = GUICtrlRead($inpMachineName)
    $RebootTime = GUICtrlRead($inpShutdownTime)
    GUICtrlSetData($lblCountdown, "Pinging " &$MachineToReboot & "...")
;~   $Pinged = Ping($MachineToReboot, 1000)
    If Not StringIsDigit($RebootTime) Then
        GUICtrlSetData($lblCountdown, "Shutdown time must be in digits.")
        ControlFocus("Remote Reboot", "", $inpShutDownTime)
        Send("+{HOME}")
    Else
;~   If Not $Pinged Then
;~       GUICtrlSetData($lblCountdown, "Cannot ping " & $MachineToReboot & ".  Check machine name and try again.")
;~       ControlFocus("Remote Reboot", "", $inpMachineName)
;~       Send("+{HOME}")
;~   Else
            GUICtrlSetState($btnAbort, $GUI_ENABLE)
            GUICtrlSetState($btnReboot, $GUI_DISABLE)
            ControlFocus("Remote Reboot", "", $btnAbort)
            GUICtrlSetData($lblCountdown, $MachineToReboot & " pinged.  Rebooting...")
            Sleep(500)
;~           _RunDOS ("shutdown -m \\" & $MachineToReboot & " -r -t " & $RebootTime)

            $count = $RebootTime
            AdlibEnable('_CountDown', 1000)
;~   EndIf
    EndIf
EndFunc

Func _CountDown()
    GUICtrlSetData($lblCountdown, "Rebooting " & $MachineToReboot & " in " & $Count & " seconds...")
    $count -= 1 
    
    If $count = 0 then 
        Msgbox(0, '', 'SHUTDOWN NOW!!!')
    EndIf
EndFunc

Func btnAbortClick()
    GUICtrlSetData($lblCountdown, "Aborting...")
;~   _RunDOS ("shutdown -m \\" & $MachineToReboot & " -a")
    GUICtrlSetData($lblCountdown, "Reboot of " & $MachineToReboot & " aborted.")
    GUICtrlSetState($btnAbort, $GUI_DISABLE)
    GUICtrlSetState($btnReboot, $GUI_ENABLE)
    AdlibDisable()
    ControlFocus("Remote Reboot", "", $inpMachineName)
    Send("+{HOME}")
EndFunc
I commented out some lines in order to get it to work on my computer... The Adlibenable/disable is the key part of what I added. Look i t up in the helpfile for more examples...

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
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...