This is kind of the same but you don't need to file install anything.
Also you do not need AutoIT installed on the PC it is running on once compiled (which some people get confused about)
This will create a temporary file to watch over your application and your application should also watch over the process monitoring script and relaunch if required.
Your script has to be compiled for this to work!
You will see that if you run the compiled code you will get 2 processes, if you end task on either one it will relaunch.
In my example below if you close the gui in anyway other than the close button (alt F4 or the top right X) the gui relaunches.
There are ways of killing both processes but for most people that need this sort of thing I think it is fine.
Edited: Added additional versions and hopefully simplified it a little
#include <GuiConstants.au3> Global $procwatchPid; you need this GuiCreate("MyGUI", 392, 208,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS)) $Button_1 = GuiCtrlCreateButton("Close", 270, 170, 110, 30) GuiSetState() $procwatchtimer = TimerInit(); create a timer so we don't thrash the CPU While 1 $msg = GuiGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exit Case $msg = $Button_1 ProcessClose($procWatchPid);remember to close the process watcher when you really want to exit Exit Case Else ;this is where we make sure the Process watcher hasn't been closed if it has been closed create a new one If TimerDiff($procwatchtimer) >=500 then _ProcWatch() $procwatchtimer = TimerInit() EndIf EndSelect WEnd ;=============================================================================== ; ; Function Name: _ProcWatch() ; Description: Monitors your application to ensure it is always running ; ;Author: Chris Lambert ;Notes: Requires a Global variable of $procwatchPid to be declared ; ;Returns @error = 1 @extended = 2 ,$procWatchPid = 0 on None compiled Script ;Returns @error = 0 @extended = 1 ,$procWatchPid = MonitoringPID on Procwatch ; Already running ;Returns @error = 0 @extended = 0 ,$procWatchPid = MonitoringPID on new ; instance ofProcwatch ; ;=============================================================================== Func _ProcWatch() Local $procWatch,$stemp If NOT @compiled then Return SetError (1,2,0) If Not IsDeclared("procWatchPid") then Exit Msgbox(0,"Error","$procwatchPid is not declared") If ProcessExists($procwatchPid) then Return SetError (0,1,$procwatchPid) $procWatch ='Opt ("TrayIconHide",1)'& @crlf & _ 'While 1' & @crlf & _ 'If Not ProcessExists("' & @AutoItPID &'") then ' & @crlf & _ 'Run ("' & FileGetShortName(@ScriptFullPath) & '","' & @scriptdir & '")' & @crlf & _ 'Exit' & @crlf & _ 'Endif' & @crlf & _ 'Sleep (500)' & @crlf & _ 'Wend' $stemp = FileOpen(@tempdir & "\procwatch.tmp",2) FileWrite($stemp,$procWatch) FileClose($stemp) $procwatchPid = Run (FileGetShortName(@AutoItExe) & " /Autoit3ExecuteScript " & @tempdir & "\procwatch.tmp", @TempDir) Return Seterror (0,0,$procwatchPid) EndFunc
Here is another example without a gui, try killing the process in task manager press "esc" to quit
Global $procWatchPid $procwatchtimer = TimerInit(); create a timer so we don't thrash the CPU HotkeySet("{esc}", "_CloseMe") While 1 ;this is where we make sure the Process watcher hasn't been closed if it has been closed create a new one If TimerDiff($procwatchtimer) >=500 then _ProcWatch() $procwatchtimer = TimerInit() EndIf Sleep (100) WEnd Func _CloseMe() ProcessClose($procWatchPid);remember to close the process watcher when you really want to exit Exit EndFunc ;=============================================================================== ; ; Function Name: _ProcWatch() ; Description: Monitors your application to ensure it is always running ; ;Author: Chris Lambert ;Notes: Requires a Global variable of $procwatchPid to be declared ; ;Returns @error = 1 @extended = 2 ,$procWatchPid = 0 on None compiled Script ;Returns @error = 0 @extended = 1 ,$procWatchPid = MonitoringPID on Procwatch ; Already running ;Returns @error = 0 @extended = 0 ,$procWatchPid = MonitoringPID on new ; instance ofProcwatch ; ;=============================================================================== Func _ProcWatch() Local $procWatch,$stemp If NOT @compiled then Return SetError (1,2,0) If Not IsDeclared("procWatchPid") then Exit Msgbox(0,"Error","$procwatchPid is not declared") If ProcessExists($procwatchPid) then Return SetError (0,1,$procwatchPid) $procWatch ='Opt ("TrayIconHide",1)'& @crlf & _ 'While 1' & @crlf & _ 'If Not ProcessExists("' & @AutoItPID &'") then ' & @crlf & _ 'Run ("' & FileGetShortName(@ScriptFullPath) & '","' & @scriptdir & '")' & @crlf & _ 'Exit' & @crlf & _ 'Endif' & @crlf & _ 'Sleep (500)' & @crlf & _ 'Wend' $stemp = FileOpen(@tempdir & "\procwatch.tmp",2) FileWrite($stemp,$procWatch) FileClose($stemp) $procwatchPid = Run (FileGetShortName(@AutoItExe) & " /Autoit3ExecuteScript " & @tempdir & "\procwatch.tmp", @TempDir) Return Seterror (0,0,$procwatchPid) EndFunc
Or using adlib instead of timers and loops
Global $procwatchPid; you need this AdlibEnable("AdlibFunc",500) HotkeySet("{esc}", "_CloseMe") While 1 Sleep (100) WEnd Func AdlibFunc() _ProcWatch() EndFunc Func _CloseMe() ProcessClose($procWatchPid);remember to close the process watcher when you really want to exit Exit EndFunc ;=============================================================================== ; ; Function Name: _ProcWatch() ; Description: Monitors your application to ensure it is always running ; ;Author: Chris Lambert ;Notes: Requires a Global variable of $procwatchPid to be declared ; ;Returns @error = 1 @extended = 2 ,$procWatchPid = 0 on None compiled Script ;Returns @error = 0 @extended = 1 ,$procWatchPid = MonitoringPID on Procwatch ; Already running ;Returns @error = 0 @extended = 0 ,$procWatchPid = MonitoringPID on new ; instance ofProcwatch ; ;=============================================================================== Func _ProcWatch() Local $procWatch,$stemp If NOT @compiled then Return SetError (1,2,0) If Not IsDeclared("procWatchPid") then Exit Msgbox(0,"Error","$procwatchPid is not declared") If ProcessExists($procwatchPid) then Return SetError (0,1,$procwatchPid) $procWatch ='Opt ("TrayIconHide",1)'& @crlf & _ 'While 1' & @crlf & _ 'If Not ProcessExists("' & @AutoItPID &'") then ' & @crlf & _ 'Run ("' & FileGetShortName(@ScriptFullPath) & '","' & @scriptdir & '")' & @crlf & _ 'Exit' & @crlf & _ 'Endif' & @crlf & _ 'Sleep (500)' & @crlf & _ 'Wend' $stemp = FileOpen(@tempdir & "\procwatch.tmp",2) FileWrite($stemp,$procWatch) FileClose($stemp) $procwatchPid = Run (FileGetShortName(@AutoItExe) & " /Autoit3ExecuteScript " & @tempdir & "\procwatch.tmp", @TempDir) Return Seterror (0,0,$procwatchPid) EndFunc
Edited by ChrisL, 07 August 2007 - 06:46 AM.





