ChrisL Posted June 29, 2007 Share Posted June 29, 2007 (edited) People often ask how can I make sure my program is always running, and the answer is fileinstall another exe to watch over it.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 littleexpandcollapse popup#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) EndFuncHere is another example without a gui, try killing the process in task manager press "esc" to quitexpandcollapse popupGlobal $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) EndFuncOr using adlib instead of timers and loopsexpandcollapse popupGlobal $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 August 7, 2007 by ChrisL [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
busysignal Posted June 30, 2007 Share Posted June 30, 2007 @ChrisL, that is a pretty creative way of doing it. This will come in handy. Cheers! Link to comment Share on other sites More sharing options...
JustinReno Posted August 3, 2007 Share Posted August 3, 2007 (edited) how exactly does this work? whats the gui for? do you need to call the function somewhere in your script because i dont see _procwatch() anywhere else in your script. EDIT: Is anyone going to respond??? Do any of you know how this works? Edited August 3, 2007 by JustinReno Link to comment Share on other sites More sharing options...
JustinReno Posted August 3, 2007 Share Posted August 3, 2007 ANYONE?!? Link to comment Share on other sites More sharing options...
ChrisL Posted August 6, 2007 Author Share Posted August 6, 2007 (edited) how exactly does this work? whats the gui for? do you need to call the function somewhere in your script because i dont see _procwatch() anywhere else in your script. EDIT: Is anyone going to respond??? Do any of you know how this works?Did you actually read/try this script?Line 3 is the first time _procWatch is called and this generates the first instance of the watcher.Line 9 creates a timer so we only check if the _procWatcher is still there after a set amount of timeLine 22 to 25 we check the _procWatch process and if it isn't there we create a new instance.Edited the examples above and simplified the UDFNo you don't need a gui all you need is:The _ProcWatch Function a Global variable to store the processwatch PIDa Timerand a loop in which you can keep checking for the _procWatch() PID or an adlibYou need to have a way of terminating the _procWatch process for when you really want to exit! Edited August 7, 2007 by ChrisL [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
ChrisL Posted August 6, 2007 Author Share Posted August 6, 2007 mr.lambert, could you explain a lil' bit further, for the sake of new users like me. _ProceesWatch() creates an Au3 script based on the main applications PID and Full path to the executable. It then runs the temporary file with it's own Autoit Exe so now you have the main program and a second program which is watching the main PID In the main program you check for the watcher PID (the temporary au3 file that we ran which is watching our main app) If the watcher PID is not present you call the _processWatch() function again to create a new watcher for the main app. But make sure you call it with a variable to store the PID in $procwatchPid = _processWatch()If the main app is killed or crashes the watcher program will relaunch the main application, if the watcher is terminated the main app will create a new watcher [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
ChrisL Posted August 14, 2007 Author Share Posted August 14, 2007 by using pressing on the 'ctrl' key, users can delete both processes simultaneously.Are you sure about that? I just tried it and I can't [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
slayerz Posted August 22, 2007 Share Posted August 22, 2007 This is what I'm looking for...Thanks ChrisL AUTOIT[sup] I'm lovin' it![/sup] Link to comment Share on other sites More sharing options...
LoWang Posted October 9, 2009 Share Posted October 9, 2009 This is great! I did not know, that every compiled script can work also as the AutoIt script interpreter! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now