Jump to content



Photo

_procWatch()


  • Please log in to reply
8 replies to this topic

#1 ChrisL

ChrisL

    Mass Spanner!

  • Active Members
  • PipPipPipPipPipPip
  • 1,746 posts

Posted 29 June 2007 - 05:21 PM

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

Plain Text         
#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

Plain Text         
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

Plain Text         
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.






#2 busysignal

busysignal

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 348 posts

Posted 30 June 2007 - 03:49 AM

@ChrisL, that is a pretty creative way of doing it. This will come in handy.

Cheers! :rolleyes:

#3 JustinReno

JustinReno

    My custom made Vortex.

  • Banned (NOT IN USE)
  • 2,330 posts

Posted 03 August 2007 - 03:56 PM

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, 03 August 2007 - 08:48 PM.


#4 JustinReno

JustinReno

    My custom made Vortex.

  • Banned (NOT IN USE)
  • 2,330 posts

Posted 03 August 2007 - 08:56 PM

ANYONE?!?

#5 ChrisL

ChrisL

    Mass Spanner!

  • Active Members
  • PipPipPipPipPipPip
  • 1,746 posts

Posted 06 August 2007 - 06:35 PM

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, 07 August 2007 - 06:49 AM.


#6 ChrisL

ChrisL

    Mass Spanner!

  • Active Members
  • PipPipPipPipPipPip
  • 1,746 posts

Posted 06 August 2007 - 07:31 PM

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

#7 ChrisL

ChrisL

    Mass Spanner!

  • Active Members
  • PipPipPipPipPipPip
  • 1,746 posts

Posted 14 August 2007 - 09:37 PM

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

#8 slayerz

slayerz

    Prodigy

  • Active Members
  • PipPipPip
  • 160 posts

Posted 22 August 2007 - 07:33 AM

This is what I'm looking for...Thanks ChrisL :)
AUTOIT I'm lovin' it!

#9 LoWang

LoWang

    Polymath

  • Active Members
  • PipPipPipPip
  • 205 posts

Posted 09 October 2009 - 03:37 PM

This is great! I did not know, that every compiled script can work also as the AutoIt script interpreter!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users