Sign in to follow this  
Followers 0
PsychOfMSE

Help with adding timeout to button press

4 posts in this topic

So I have a program that updates software on an end user device.  When it comes time for a reboot it will utilize the following script:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$r = "2"
_main()

Func _main()
$MyGUI = GUICreate("Reboot Needed", 493, 120, 192, 114)
$Message = GUICtrlCreateLabel("Windows needs to reboot your system to complete system changes. Please save your work and click" & @CRLF & "Reboot. Or click Delay to Postpone the operation for 30 minutes. You may postpone " & $r & " more times.", 0, 8, 601, 75)
$Button1 = GUICtrlCreateButton("Reboot", 50, 70, 153, 41)
$Button2 = GUICtrlCreateButton("Delay", 300, 70, 137, 41)
GUISetState(@SW_SHOW)
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   _main()
  Case $Button1
    Shutdown(6)
  Case $Button2
   GUIDelete()
   _delay()
 EndSwitch
WEnd
EndFunc

Func _delay()
 $r -= 1
 Sleep(10000)
 _main()
EndFunc

What I need is a timeout on this box.  Say if the user doesn't select Reboot or Delay within 30 seconds it will default to rebooting the device.  Is this possible and if so how would I go about it?

Thanks!

Share this post


Link to post
Share on other sites



Look at TimerInit and TimerDiff in the help file and use that to determine when 30 seconds have passed.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Needed something to do...

This is one way of going at it, the counter is not showing up correctly in the GUI for some reason... Blocked some semi invisible frame or something. . Not sure why but the rest works:

Edit: Fixed Thanks BrewmanNH   :)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Local $i
Local $label5
$r = "2"
_main()

Func _main()
$MyGUI = GUICreate("Reboot Needed", 493, 120, 192, 114)
$Message = GUICtrlCreateLabel("Windows needs to reboot your system to complete system changes. Please save your work and click" & @CRLF & "Reboot. Or click Delay to Postpone the operation for 30 minutes. You may postpone " & $r & " more times.", 0, 8, 601, 50)
$Button1 = GUICtrlCreateButton("Reboot", 50, 70, 153, 41)
$Button2 = GUICtrlCreateButton("Delay", 300, 70, 137, 41)
$label4 = GUICtrlCreateLabel( "Will Automatically reboot in: ", 120, 45, 130, 25)
$label5 = GUICtrlCreateLabel($i, 290, 45, 30, 20)
GUISetState(@SW_SHOW)

_30sec()
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   _main()
  Case $Button1
    Shutdown(6)
  Case $Button2
   GUIDelete()
   _delay()
 EndSwitch
WEnd
EndFunc

Func _30sec()

    For $i = 30 To 1 Step -1
        Sleep(1000)
        GUICtrlSetData($label5, $i)
        ConsoleWrite("$i =  " & $i & @CRLF)
        If $i = 1 Then
            ; Do whatcha gotta do...
            Endif
       Next
EndFunc

Func _delay()
 $r -= 1
 Sleep(10000)
 _main()
EndFunc
Edited by billo

Share this post


Link to post
Share on other sites

Taking billo's script as a starting point, I've boiled it down to one function that should do exactly what you need.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
_main()

Func _main()
     Local $nOldTimer = 0, $nDelay = 0
     $MyGUI = GUICreate("Reboot Needed", 493, 120, 192, 114)
     $Message = GUICtrlCreateLabel("Windows needs to reboot your system to complete system changes. Please save your work and click" & @CRLF & "Reboot. Or click Delay to Postpone the operation for 30 minutes. You may postpone " & $r & " more times.", 0, 8, 601, 50)
     $Button1 = GUICtrlCreateButton("Reboot", 50, 70, 153, 41)
     $Button2 = GUICtrlCreateButton("Delay", 300, 70, 137, 41)
     $label4 = GUICtrlCreateLabel("Will Automatically reboot in: ", 120, 45, 130, 25)
     $label5 = GUICtrlCreateLabel($i, 290, 45, 30, 20)
     GUISetState()

     $hTimer = TimerInit()
     While 1
          $nMsg = GUIGetMsg()
          Switch $nMsg
               Case $GUI_EVENT_CLOSE
                    ExitLoop
               Case $Button1
                    Shutdown(6)
               Case $Button2
                    If $nDelay < 2 Then
                         $hTimer = TimerInit()
                         $nDelay += 1
                    EndIf
          EndSwitch
          $nTimer = 30 - Int(TimerDiff($hTimer) / 1000)
          If $nTimer = 0 Then
               ; Do whatcha gotta do
          Else
               If $nTimer <> $nOldTimer Then
                    GUICtrlSetData($label5, $nTimer)
                    $nOldTimer = $nTimer
               EndIf
          EndIf
          Sleep(100)
     WEnd
     Exit
EndFunc   ;==>_main

BTW, don't use Local when declaring Global variables, declaring a Local variable in a Global scope doesn't make them local, they're Global.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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