MadDogDean Posted February 10, 2011 Share Posted February 10, 2011 (edited) 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: expandcollapse popup#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 February 10, 2011 by MadDogDean Link to comment Share on other sites More sharing options...
Tvern Posted February 10, 2011 Share Posted February 10, 2011 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 More sharing options...
MadDogDean Posted February 10, 2011 Author Share Posted February 10, 2011 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 More sharing options...
Tvern Posted February 10, 2011 Share Posted February 10, 2011 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 More sharing options...
MadDogDean Posted February 11, 2011 Author Share Posted February 11, 2011 Tvern, Thanks for the suggestions. I ended up running 2 independent exe files and set up the actions to cascade from one through to the other. Works like a charm! Cheers, Dean 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