1/3 How to Check opened PID With Timer and Array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Awannaknow
Posts: 78
Joined: 01 Oct 2013, 08:07

1/3 How to Check opened PID With Timer and Array?

25 Mar 2017, 04:03

Cannot find what is wrong in this code, can some members put me on tracks here, thanks in advance.
First time trying to struggle with objects . . .
I'm trying to make this script open processes or files and check PID, when one file is closed, script must take it into account.
When none left, script closes.
This , will open files and check if they are opened, until there is no more, then close.
PBM:When closing them, script only "see" them closing if starting from the last opened, ie:
---------
open "01.txt", then "02.txt", "abc.exe", and "03.txt"
"See" them closing only if first to be closed is last opened: "03.txt", then any one will do.
If an other one is closed, it correct the opened files number only when they're all closed.
---------
First I tried with process, WaitClose, but if I remember correctly, the script is not responding anymore to add others files to check until the file is closed and the WaitClose command finished. It has to be #SingleInstance Off to open others individual instances for each opened files, but only works when the first instance is "unlocked".
WinWait is not what I need, as I check for PID.
Although the script above is working, I need to continue as it is not the solution I need, so I decided to make a continuation thread here:
2/3 How to check multiple processes?
---------
Update - April 2 2017: Working code with a hotkey from the post below:

Code: Select all

!a::
FileSelectFile, OutputVar
Run, %OutputVar%,,,OutputVarPID
if !ProcOnDuty
ProcOnDuty:=Array()
ProcOnDuty.insert(OutputVarPID)
OutputVarPID=
MaxIndex := ProcOnDuty.MaxIndex()
SetTimer, ReCheck
Return

ReCheck:
For index, value in ProcOnDuty
{
Process, Exist, %value%
{
if !errorlevel
{
ProcOnDuty.Remove(index)
MaxIndex := ProcOnDuty.MaxIndex()
If (MaxIndex>0)
MsgBox % "Value: " value " MaxIndex: " MaxIndex " ProcOnDuty.MaxIndex(): " ProcOnDuty.MaxIndex()
; Do whatever you want to do here if you need it
Else
{
SetTimer, ReCheck, Off
;MsgBox,no more exec on duty MaxIndex: %MaxIndex%
; Do whatever you want to do here if you need it
ExitApp
}
}
}
}
return
return
A working code without hotkey/array/timer, opening script multiple time, from 3/3 How can I do this?:

Code: Select all

#NoEnv 
#SingleInstance off 
;#NoTrayIcon
#Persistent 
SendMode Input 
DetectHiddenWindows, On 
Create_files_and_shortcuts() 
return 
Create_files_and_shortcuts() { 
    global 
    ShortcutArg=%1% 
    if (!ShortcutArg)
        ShortcutArg=%1% 

    File1=%A_ScriptDir%\12345.txt 
    Shortcut1=%A_ScriptDir%\12345.txt.lnk 
    File2=%A_ScriptDir%\678910.txt 
    Shortcut2=%A_ScriptDir%\678910.txt.lnk 
    File3=%A_ScriptDir%\1a2b3c.txt 
    Shortcut3=%A_ScriptDir%\1a2b3c.txt.lnk 
    if (ShortcutArg || MaxIndex>0) { 
        GoSub, OpenFiles 
    } else { 
        if (FileExist(File1)) { 
        } else { 

;------ 1- Files & shortcut creation - Start - ------  
            Msgbox,  
            ( 
            This script will create 3 empty txt files and their modified shortcuts.  
            When opening them by double clicking on shortcuts, the script is supposed to check opened Process ID (PID).  
            According to your action on closing or reopening the files via the shortcuts the script will increase and decrease the number of opened files. 
            And finally the script close when the 3 files will be closed. 
            ) 
            FileAppend,, %File1% 
        } 
        if (FileExist(Shortcut1)) { 
        } else { 
            FileCreateShortcut, %A_AhkPath%, %Shortcut1% , %A_ScriptDir%, "%A_ScriptFullPath%" "%A_ScriptDir%\12345.txt", Modified 12345.txt Shortcut 
        } 
        if (FileExist(File2)) { 
        } else { 
            FileAppend,, %File2% 
        } 
        if (FileExist(Shortcut2)) { 
        } else { 
            FileCreateShortcut, %A_AhkPath%, %Shortcut2% , %A_ScriptDir%, "%A_ScriptFullPath%" "%A_ScriptDir%\678910.txt", Modified 678910.txt Shortcut 
        } 
        if (FileExist(File3)) { 
        } else { 
            FileAppend,, %File3% 
        } 
        if (FileExist(Shortcut3)) { 
        } else { 
            FileCreateShortcut, %A_AhkPath%, %Shortcut3% , %A_ScriptDir%, "%A_ScriptFullPath%" "%A_ScriptDir%\1a2b3c.txt", Modified 1a2b3c.txt Shortcut 
        } 
        if (File1 || File2 || File3 || Shortcut1 || Shortcut2 || Shortcut3) { 
        } else { 
             
        } 
        ExitApp 
    } 
    return 
} 
;------ 1- Files & shortcut creation - End - ------
;------ 2- Double click on Shortcut to open files - Start - ------
OpenFiles:  
        if (ShortcutArg= File1 || ShortcutArg= File2 || ShortcutArg=File3) { 
            RunWait, %ShortcutArg%,,, OutputVarPID 
            ExitApp 
        } 
return 
;------ 2- Double click on Shortcut to open files - End - ------ 
Last edited by Awannaknow on 02 Apr 2017, 01:34, edited 6 times in total.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to Check opened PID With Timer and Array?

25 Mar 2017, 10:56

Don't know if it's exactly what you aim for but here's a solution. I try to adhere to your code to make the change the less substantial possible. Hope this helps!

Code: Select all

!a::
FileSelectFile, OutputVar
Run, %OutputVar%,,,OutputVarPID
if !ProcOnDuty
ProcOnDuty:=Array()
if !i
i := 0
ProcOnDuty.insertAt(++i, {"PID": OutputVarPID, "path": OutputVar}) ; set ProcOnDuty[i] to be for exemple {"PID": 3472, "path": "C://desktop/file.txt"}
SetTimer, ReCheck
Return

ReCheck:
For __index, __object in ProcOnDuty ; loop throught the array's content where each key's value is itself an object with a "PID" key and a "path" key
{
Process, Exist, % __object.PID

	if not (errorlevel)
	{
	removedkey := ProcOnDuty.Removeat(__index)

		
		If (ProcOnDuty.MaxIndex()>0)
		{
		j := 1
		for __index in ProcOnDuty
		j++
		MsgBox % "-02- PID: " removedkey.PID . "with path" removedkey.path " is closed, now # of opened files are: " j
		}
		else
		{
		MsgBox, 16,, test
		SetTimer, ReCheck, Off
		ExitApp
		}
		
	}
}
return
EDIT added j variable instead of ProcOnDuty.MaxIndex() to check the number of opened files
my scripts
Awannaknow
Posts: 78
Joined: 01 Oct 2013, 08:07

Re: How to Check opened PID With Timer and Array?

25 Mar 2017, 12:02

Thanks A_AhkUser,
I just wanted to open anything with run command, like files or exe, check if they are still opened, update array when they are closed, close the script when no more are opened.
Of course it is a short simplified version to focus on the pbm only.
I added ; Do whatever you want to do here if you need it comment to show something could be done other than just checking for the closing of the files.
I checked your code I couldn't make msgbox firing, but I appreciate to read code from someone else, it always help.
AND I understood it because as you wrote: "I try to adhere to your code to make the change the less substantial possible"
I just need a simple array, with just process ID as values and index that act as key, not an associative array with key and value Using the {key:value} notation,.
But I suppose it doesn't change much of anything.
The values order's in the array is of no importance, as they're all cycled one after each other, nor a variable to remember the closed one.
I think I found my mistakes and solved the pbm, I'm not sure if my gibberish code is ok, nor if it is the right way to do it, but it works, maybe a brave soul could tell me if it is right enough:
Although the script above is working, I need to continue as it is not the solution I need, so I decided to make a continuation thread here:
2/3 How to check multiple processes?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], Joey5, Nerafius, RandomBoy and 155 guests