Solved

Infinite AutoIT Loop, Exit Compiled EXE to get Command Prompt Back

Posted on 2013-12-02
7
1,502 Views
Last Modified: 2013-12-03
I am deploying a compiled AutoIT exe script via SCCM, however I have an infinite LOOP incorporated at the end of the script to display a message in the System Tray.  Via a Command Prompt, I have noticed that the EXE is waiting for the loop to complete before returning the Command Prompt back.  Is there a way to have the loop run infinitely and have the EXE return (an Exit Code of 0) and the command prompt with out breaking the loop?

The script with the infinite loop is below:

TraySetIcon("ActionCenter.dll",2)
TraySetToolTip ("Please restart your system")
Break(0) ;Disable break

While 1 = 1
   TrayTip("IT", "Critical updates have been installed on your system. Please save your work and restart your system as soon as possible.", 10, 1)
   Sleep (900000)
WEnd
0
Comment
Question by:rmessing171
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:mlongoh
ID: 39691389
Well if it doesn't exit then it's not going to generate an exit code.  I think you need to just launch it and that's that.  Otherwise, you need to create another autoit script that runs the second, and it shouldn't exit cleanly until you see that the one you want to leave running has started.
0
 

Author Comment

by:rmessing171
ID: 39691455
Thank you for your quick response!  I would like to pursue the creation of another autoit script that runs the second.  How would I go about doing this?  Could you provide me a quick example of how I would call the second AutoIT script and script it so it doesn't exit cleanly until I see that the script I wanted has started? Please forgive me, I am new at AutoIT and nesting scripts.
0
 
LVL 29

Accepted Solution

by:
matrixnz earned 500 total points
ID: 39692114
Not sure why you're using command prompt, you should be able to just call the script directly.  I created a restart script that may help, it's actually based on Windows Updates and checks the following keys for pending restarts.
HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile  - Flags
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager - PendingFileRenameOperations

You can either use the following syntax:
_FNC_RESTART("Application Name", 1), request restart
_FNC_RESTART("Application Name", 0), check if restart is required.

Script is x86/x64 compatible and tested on Windows XP/7

Pending Restart Detected
#NoTrayIcon

Opt("TrayMenuMode", 1)

#AutoIt3Wrapper_Res_File_Add=Resources\Restart.png,10,IMG_RESTART
#include <Array.au3>
#include <ComboConstants.au3>
#include <Resources.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <StringSize.au3>
#include <WinAPI.au3>

Global $GLB_sGUIFONT = 'Trebuchet MS'
Global $GLB_TRAYSET = 0
Global $VAR_iGUITIME = 0
Global $GUI_hGUINAME
Global $BTN_hGUIEXIT
Global $BTN_hGUIWAIT
Global $BTN_hRESTART

_FNC_RESTART('Enter Application Name Here...')

Func _FNC_RESTART($PAR_sPRODUCT, $PAR_vRESTART = 1)
  If $PAR_vRESTART = 1 Then
    Local $CHK_iRESTART = 0
    RegRead('HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile', 'Flags')
      If Not @error Then $CHK_iRESTART += 1
    RegRead('HKLM64\SOFTWARE\Microsoft\Updates\UpdateExeVolatile', 'Flags')
      If Not @error Then $CHK_iRESTART += 1
    RegRead('HKLM\SYSTEM\CurrentControlSet\Control\Session Manager', 'PendingFileRenameOperations')
      If Not @error Then $CHK_iRESTART += 1
    RegRead('HKLM64\SYSTEM\CurrentControlSet\Control\Session Manager', 'PendingFileRenameOperations')
      If Not @error Then $CHK_iRESTART += 1
    If $CHK_iRESTART = 0 Then Return
  EndIf

  Local $VAR_POSITION_ARRAY = WinGetPos('[CLASS:Shell_TrayWnd]')
  If $VAR_POSITION_ARRAY[1] = 0  Then
    $VAR_POSITION_ARRAY[0] = @DesktopWidth
    $VAR_POSITION_ARRAY[1] = @DesktopHeight
    $VAR_POSITION_ARRAY[2] = 0
    $VAR_POSITION_ARRAY[3] = 0
  EndIf

  Local $ARR_sMESSAGE[4][3]
    $ARR_sMESSAGE[0][0] = 3
    $ARR_sMESSAGE[1][0] = 'Pending Restart Detected'
    $ARR_sMESSAGE[1][1] = 'Please restart your computer before installing ' & @CRLF & $PAR_sPRODUCT
    $ARR_sMESSAGE[1][2] = $PAR_sPRODUCT & ' has detected that there are pending system changes that require a reboot.  Please save your files, reboot the system and rerun the installation.'
    $ARR_sMESSAGE[2][0] = 'Restart Required'
    $ARR_sMESSAGE[2][1] = 'Please restart your computer to complete the installation of ' & $PAR_sPRODUCT
    $ARR_sMESSAGE[2][2] = $PAR_sPRODUCT & ' has detected that there are pending system changes that require a reboot to complete the installation.  Please save your files and reboot the system.'

  Local $VAR_iBNRSIZE = _StringSize($ARR_sMESSAGE[$PAR_vRESTART][1], 10, 800, 0, $GLB_sGUIFONT)
  ;~ $GUI_iBNRSIZE[0] = Array Count
  ;~ $GUI_iBNRSIZE[1] = x Coordinates
  ;~ $GUI_iBNRSIZE[2] = y Coordinates
  ;~ $GUI_iBNRSIZE[3] = w Width
  ;~ $GUI_iBNRSIZE[4] = h Height
  Local $GUI_iBNRSIZE[5] = [4, 0, 0, 335, 60]
  If $VAR_iBNRSIZE[2] > $GUI_iBNRSIZE[3] Then $GUI_iBNRSIZE[3] = $VAR_iBNRSIZE[2]

  $GUI_hGUINAME = GUICreate($ARR_sMESSAGE[$PAR_vRESTART][0], 60 + $GUI_iBNRSIZE[3] + 10, 200, ($VAR_POSITION_ARRAY[0] + $VAR_POSITION_ARRAY[2]) - (90 + $GUI_iBNRSIZE[3]), $VAR_POSITION_ARRAY[1] - 240)
    GUISetBkColor(0xFFFFFF)
    GUISetFont(8.5, 400, 0, $GLB_sGUIFONT)

  GUICtrlCreateGraphic(0, 0, $GUI_iBNRSIZE[4] + $GUI_iBNRSIZE[3] + 10, $GUI_iBNRSIZE[4])
    GUICtrlSetBkColor(-1, 0xFFCC33)
    GUICtrlSetState(-1, $GUI_DISABLE)

  $GUI_hGUIICON = GUICtrlCreatePic('', 0, 0, $GUI_iBNRSIZE[4] + $GUI_iBNRSIZE[3] + 10, $GUI_iBNRSIZE[4])
    _ResourceSetImageToCtrl($GUI_hGUIICON, 'IMG_RESTART')
    GUICtrlSetState($GUI_hGUIICON, $GUI_DISABLE)
    GUICtrlSetResizing($GUI_hGUIICON, $GUI_DOCKALL)

  $GUI_hLBLNAME = GUICtrlCreateLabel($ARR_sMESSAGE[$PAR_vRESTART][1], $GUI_iBNRSIZE[4], ($GUI_iBNRSIZE[4] - $VAR_iBNRSIZE[3])/2, $GUI_iBNRSIZE[3], $VAR_iBNRSIZE[3])
  GUICtrlSetFont($GUI_hLBLNAME, 10, 800, 0, $GLB_sGUIFONT)
  GUICtrlSetBkColor($GUI_hLBLNAME, $GUI_BKCOLOR_TRANSPARENT )

  GUICtrlCreateLabel($ARR_sMESSAGE[$PAR_vRESTART][2], 60, 65, $GUI_iBNRSIZE[3], 60)

  GUICtrlCreateLabel('Remind me in:', 60 + ($GUI_iBNRSIZE[3] - 250), 125, 100, 25, $SS_CENTERIMAGE)
  $GUI_hGUITIME = GUICtrlCreateCombo('', 60 + ($GUI_iBNRSIZE[3] - 150), 125, 150, 25, $CBS_DROPDOWNLIST)
    GUICtrlSetData($GUI_hGUITIME, '10 minutes|1 hour|4 hours', '10 minutes')

  GUICtrlCreateGraphic(0, 160, $GUI_iBNRSIZE[4] + $GUI_iBNRSIZE[3] + 10, 40)
  GUICtrlSetBkColor(-1, 0xF0F0F0)
  GUICtrlSetColor(-1, 0xDFDFDF)
  GUICtrlSetState(-1, $GUI_DISABLE)

  If $PAR_vRESTART = 1 Then $BTN_hGUIEXIT = GUICtrlCreateButton('Cancel', 60 + ($GUI_iBNRSIZE[3] - 300), 168, 100, 24)
  $BTN_hRESTART = GUICtrlCreateButton('Restart now', 60 + ($GUI_iBNRSIZE[3] - 200), 168, 100, 24)
  $BTN_hGUIWAIT = GUICtrlCreateButton('Postpone', 60 + ($GUI_iBNRSIZE[3] - 100), 168, 100, 24)

  GUISetState()
  AdlibRegister('_FNC_POSTPONE', 1000)
  While 1

    $MSGTRAY = TrayGetMsg()
    Switch $MSGTRAY
      Case $TRAY_EVENT_PRIMARYUP
        If $GLB_TRAYSET = 1 Then
          TraySetState(2)
          GuiSetState(@SW_RESTORE, $GUI_hGUINAME)
          GuiSetState(@SW_SHOW, $GUI_hGUINAME)
          $GLB_TRAYSET = 0
        EndIf
    EndSwitch
    Switch GUIGetMsg()
      Case $BTN_hGUIEXIT
        Exit
      Case $GUI_EVENT_CLOSE
        $VAR_iGUITIME = 600000
        If $GLB_TRAYSET = 0 Then
          GuiSetState(@SW_MINIMIZE, $GUI_hGUINAME)
          GuiSetState(@SW_HIDE, $GUI_hGUINAME)
          TraySetState(1)
          $GLB_TRAYSET = 1
        EndIf
      Case $BTN_hGUIWAIT
        Switch GUICtrlRead($GUI_hGUITIME)
          Case '10 minutes'
            ;~   600000ms = 10 minutes
            $VAR_iGUITIME = 6000
            If $GLB_TRAYSET = 0 Then
              GuiSetState(@SW_MINIMIZE, $GUI_hGUINAME)
              GuiSetState(@SW_HIDE, $GUI_hGUINAME)
              TraySetState(1)
              $GLB_TRAYSET = 1
            EndIf
          Case '1 hour'
          ;~  3600000ms = 1 hour
            $VAR_iGUITIME = 3600000
            If $GLB_TRAYSET = 0 Then
              GuiSetState(@SW_MINIMIZE, $GUI_hGUINAME)
              GuiSetState(@SW_HIDE, $GUI_hGUINAME)
              TraySetState(1)
              $GLB_TRAYSET = 1
            EndIf
          Case '4 hours'
            ;~ 14400000ms = 4 hours
            $VAR_iGUITIME = 14400000
            If $GLB_TRAYSET = 0 Then
              GuiSetState(@SW_MINIMIZE, $GUI_hGUINAME)
              GuiSetState(@SW_HIDE, $GUI_hGUINAME)
              TraySetState(1)
              $GLB_TRAYSET = 1
            EndIf
        EndSwitch
      Case $BTN_hRESTART
        Shutdown(22, $ARR_sMESSAGE[$PAR_vRESTART][0])
    EndSwitch
  WEnd
EndFunc

Func _FNC_POSTPONE()
  If $VAR_iGUITIME > 0 Then
    $VAR_iGUITIME -= 1000
    Return
  Else
    If $GLB_TRAYSET = 1 Then
      TraySetState(2)
      GuiSetState(@SW_RESTORE, $GUI_hGUINAME)
      GuiSetState(@SW_SHOW, $GUI_hGUINAME)
      $GLB_TRAYSET = 0
    EndIf
  EndIf
EndFunc

Open in new window

Restart.png
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:rmessing171
ID: 39692790
Thank you very much for this script, this is exactly what I require!  Unfortunately, I do not have the Resources.au3 and StringSize.au3 and files in my C:\Program Files (x86)\AutoIt3\Include folder.  Is there another place I can obtain them?
0
 
LVL 29

Expert Comment

by:matrixnz
ID: 39693425
Sorry about that, I've attached the versions I have, these were written by other AutoIT members, if you want to try the latest you can use the urls below, although I don't know if any newer changes have been made which could break the script.

Resources.au3 - http://www.autoitscript.com/forum/topic/51103-resources-udf/
StringSize.au3 - http://www.autoitscript.com/forum/topic/114034-stringsize-m23-new-version-16-aug-11/
Resources.txt
StringSize.txt
0
 

Author Comment

by:rmessing171
ID: 39693529
Thank you!  This helped me out tremendously!
0
 
LVL 29

Expert Comment

by:matrixnz
ID: 39693654
No worries, please note that for testing I did change the variable $VAR_iGUITIME = 600000 to 6000 (6 seconds) so I didn't have to wait for 10 minutes, but forgot to change it back.
Change:
;~   600000ms = 10 minutes
            $VAR_iGUITIME = 6000
To:
;~   600000ms = 10 minutes
            $VAR_iGUITIME = 600000

Cheers
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hyper-V not working after Anniversary Update 7 77
accessing the windows\csc folder 5 107
AD backup 6 58
What does != "" mean in programming 8 34
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question