Note: You can close the batch file using either the PID returned by _AlwaysRun() (which you'll have to store somewhere e.g. INI file) OR using the Task Manager to manually close the process.
Function: Using a Batch file. Save as _AlwaysRun.au3
#include-once #include <Constants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _AlwaysRun ; Description ...: Monitor when a program closes and then restart it. ; Syntax ........: _AlwaysRun($sFilePath[, $sCommandline = '']) ; Parameters ....: $sFilePath - FilePath of the program to monitor and restart. ; $sCommandline - [optional] A commandline string to pass to the program on restart. Default is ''. ; Return values .: Success - Returns the PID of the batch file. ; Failure - Returns 0 & sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _AlwaysRun($sFilePath, $sCommandline = '') If $sFilePath = @ScriptFullPath And @Compiled = 0 Then Return SetError(1, 0, 0) EndIf Local Const $STR_NOCASESENSEBASIC = 2 Local Const $sAppName = StringTrimLeft($sFilePath, StringInStr($sFilePath, '\', $STR_NOCASESENSEBASIC, -1)) Local $sFileName = StringReplace(@ScriptName, StringTrimLeft(@ScriptName, StringInStr(@ScriptName, '.', $STR_NOCASESENSEBASIC, -1) - 1), '') While FileExists(@TempDir & '\' & $sFileName & '.bat') $sFileName &= Chr(Random(65, 122, 1)) WEnd If $sCommandline Then $sCommandline = ' ' & $sCommandline EndIf Local Const $sData = ':START' & @CRLF _ & 'PING -n 2 127.0.0.1 > nul' & @CRLF _ & @CRLF _ & 'TASKLIST /NH /FI "IMAGENAME EQ ' & $sAppName & '" | FIND /I "' & $sAppName & '" >nul && GOTO START' & @CRLF _ & 'GOTO END' & @CRLF _ & ':END' & @CRLF _ & 'START "_AlwaysRun" "' & $sFilePath & '"' & $sCommandline & @CRLF _ & 'GOTO START' & @CRLF Local Const $hFileOpen = FileOpen(@TempDir & '\' & $sFileName & '.bat', $FO_OVERWRITE) If $hFileOpen = -1 Then Return SetError(2, 0, 0) EndIf FileWrite($hFileOpen, $sData) FileClose($hFileOpen) Return Run(@TempDir & '\' & $sFileName & '.bat', @TempDir, @SW_HIDE) EndFunc ;==>_AlwaysRun
Function: Using a VBScript file. Save as _AlwaysRun.au3
; Discontinued for now.
Example_1:
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 #include <Constants.au3> #include '_AlwaysRun.au3' ; Compile the following script before running. If @Compiled Then Example() EndIf Func Example() Local $iPID = IniRead(@ScriptDir & '\_AlwaysRunPID.ini', 'Monitor', 'PID', 0) ; Check if the monitoring process is running. If not then call the function _AlwaysRun. (This is the reason why the PID was saved to an INI file.) If ProcessExists($iPID) = 0 Then ; Start monitoring the current script path for when it closes and restart if necessary. No commandline parameters are passed. $iPID = _AlwaysRun(@ScriptFullPath, '') If @error Then Exit MsgBox($MB_SYSTEMMODAL, '_AlwaysRun()', 'The running script must be a compiled exe to work correctly.') EndIf ; Save the PID returned by _AlwaysRun to an INI file in case we need to close the monitoring process in the future. IniWrite(@ScriptDir & '\_AlwaysRunPID.ini', 'Monitor', 'PID', $iPID) Else Local Const $aArray[2] = ['No', 'Yes'] If MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), '_AlwaysRun Running: ' & $aArray[Number(ProcessExists($iPID) > 0)], 'Would you like to continue running the _AlwaysRun Example?') = 7 Then FileDelete(@ScriptDir & '\_AlwaysRunPID.ini') Exit ProcessClose($iPID) EndIf EndIf Exit 0 EndFunc ;==>Example
Edited by guinness, 04 April 2013 - 10:22 AM.







