Jump to content

Watching 2 processes simultaneously


Recommended Posts

Hello all,

I tried to search for this and got nowhere. I also posted a question in an older post, but I think it is long forgotten...

What I am trying to figure out is how to watch 2 processes from 1 AutoIt script.

Basically, I want to watch MyProgram.exe and if it closes, it's temporary files are deleted.

Also, I want to watch App1.exe. If it closes, I want to force MyProgram.exe to close and delete the MyProgram.exe temporary files.

Here's a snippet of what I tried:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_Fileversion=1.25.0.2
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Opt("TrayIconHide", 1)
#include <WinApi.au3>
If $Cmdline[0] <> 5 Then
    Exit (10)
Else
    $path = $Cmdline[1] ;external app (MyProgram.exe) was run from here
    $path2 = $Cmdline[2] ;folder #2
    $path3 = $Cmdline[3] ; folder #3
    $PID = $Cmdline[4] ;PID for external app (MyProgram.exe)
    $tempfile = $Cmdline[5] ; name of temp file
EndIf

$iPID=$PID ; $PID is from MyProgram.exe
$hProcess = _WinAPI_OpenProcess(0x00100000, False, $iPId) ; SYNCHRONIZE 
_WinAPI_WaitForSingleObject($hProcess, -1); - from MyProgram.exe


$iPId2 = ProcessExists("App1.exe")
$hProcess2 = _WinAPI_OpenProcess(0x00100000, False, $iPId2) ; SYNCHRONIZE 
_WinAPI_WaitForSingleObject($hProcess2, -1); - from App1.exe
_WinAPI_CloseHandle($hProcess)





Sleep(250)
$temp=String($tempfile&".abc")
;--if App1.exe is closed, kill MyProgram.exe and delete the files below
;--if MyProgram.exe is closed delete the files below
Sleep(250)
;FileDelete($path&"\*.*")
;ConsoleWrite("Ready to delete folders" & @CRLF)
FileSetAttrib($path2, "-RASHNOT", 1)
FileSetAttrib($path3, "-RASHNOT", 1)
FileSetAttrib($path, "-RASHNOT", 1)
DirRemove($path2, 1)
DirRemove($path3, 1)
DirRemove($path, 1)

sleep(5000)

FileDelete(@TempDir & $temp)

Sleep(250)
Exit

I am thinking I need to work this into an array... somehow...

Any thoughts out there?

Cheers,

Dean

Edited by MadDogDean
Link to comment
Share on other sites

The problem is that blocking functions (usually they have "wait" in their names -> _WinAPI_WaitForSingleObject, WinWait...() etc.) do not return untill conditions are met, meaning that you can't do anything else before it returns, making it impossible to run two wait functions at once.

The trick is to call non-blocking functions in a loop. Most blocking functions have a non-blocking equivalent. The ones that don't can usually be modified to be non-blocking.

While your WinApi solution is probably higher in performance if you get it to work, this would be a lot easier to use:

While ProcessExists("MyProgram.exe") ;if the program closes the loop ends.
    If Not ProcessExists("App1.exe") Then ProcessClose("MyProgram.exe") ; if App1 closes, the program is closes and when it does the loop ends.
    Sleep(100)
WEnd
;filedelete stuff here
Link to comment
Share on other sites

Hi Tvern,

Thanks for the input. I thought that was the problem, but there are greater minds than mine out there...

I really liked the WinAPI solution. It is truly incredible to watch _WinAPI_WaitForSingleObject sitting in the Task Manager with 0% CPU utilization!

Maybe I'll set up 2 independent apps each watching something different using the _WinAPI_WaitForSingleObject, but we'll see, I'm not sure.

Thanks again for the info.

Groetjes,

Dean

Link to comment
Share on other sites

I really liked the WinAPI solution. It is truly incredible to watch _WinAPI_WaitForSingleObject sitting in the Task Manager with 0% CPU utilization!

You can reduce the CPU usage by increasing the sleep, but it will decrease the detection speed as the processes will be cheked less often.

_WinAPI_WaitForMultipleObjects might be exactly what you need if you want cpu friendly performance.

Link to comment
Share on other sites

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