Jump to content

_procWatch()


ChrisL
 Share

Recommended Posts

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 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
Link to comment
Share on other sites

  • 1 month later...

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 by JustinReno
Link to comment
Share on other sites

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 time

Line 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 UDF

No you don't need a gui all you need is:

The _ProcWatch Function a Global variable to store the processwatch PID

a Timer

and a loop in which you can keep checking for the _procWatch() PID or an adlib

You need to have a way of terminating the _procWatch process for when you really want to exit!

Edited by ChrisL
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 years later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...