Jump to content

Detect specific process ID and wait until process finishes


Recommended Posts

I use

Run("explorer.exe " & @ScriptDir&"DataText_received_files");

How can I detect this specific explorer window process id and wait until this specific explorer window is closed?

ProcessExists does not seem to work for specific window, unless I am doing something wrong

Edited by neazoi
Link to comment
Share on other sites

The Run command returns the PID so you should be able to monitor that.

Jos

​I can't. A little help is appreciated.

Here is my code snipet.

Local $previewWin = Run("explorer.exe " & @ScriptDir&"\DataText_received_files");open explorer to see extracted files in folder
            
            While ProcessExists($previewWin)
               
            Wend
            
            DirRemove(@ScriptDir&"\DataText_received_files",1); when files have been viewed, delete the DataText_received_files dir
            DirCreate(@ScriptDir&"\DataText_received_files");recreate the DataText_received_files dir

Also the next code did not work satisfactorily. The while loop is not good as well. I want to say: as long as this explorer window is open wait (if it closes then continue script)

Run("explorer.exe " & @ScriptDir&"\DataText_received_files");open explorer to see extracted files in folder
            
            Local $hWnd = WinWait("[CLASS:Explorer]", "", 60); Wait seconds for the window to appear.
            Local $iPID = WinGetProcess($hWnd); Retrieve the PID of process using the window handle returned by WinWait.
            
            While ProcessExists($iPID)
               Sleep(100)
            Wend
            
            WinClose($hWnd)
            
            DirRemove(@ScriptDir&"\DataText_received_files",1); when files have been viewed, delete the DataText_received_files dir
            DirCreate(@ScriptDir&"\DataText_received_files");recreate the DataText_received_files dir

 

Edited by neazoi
Link to comment
Share on other sites

Explorer.exe handles all windows.

 

Use ShellExecute and wait for the window by it's title.

Exactly, that is the problem. ​Can you please give me a short example code for this?

Edited by neazoi
Link to comment
Share on other sites

When I run explorer.exe with a directory as parameter, the PID returned by run is not PID used by the explorer window.
You can try something like this :

$sDir = @ScriptDir & "\DataText_received_files"
Run("explorer.exe " & $sDir)
$hExplorer = WinWait("[REGEXPTITLE:(?i)\Q" & $sDir & "\E$]")
WinWaitClose($hExplorer)

 

Link to comment
Share on other sites

When I run explorer.exe with a directory as parameter, the PID returned by run is not PID used by the explorer window.
You can try something like this :

$sDir = @ScriptDir & "\DataText_received_files"
Run("explorer.exe " & $sDir)
$hExplorer = WinWait("[REGEXPTITLE:(?i)\Q" & $sDir & "\E$]")
WinWaitClose($hExplorer)

 

​This worked fine!

However Windows have the option to set the explorer window title to be the full path or just the name of the last folder you are in.

For example in my case, I had to do it like this:

Local $sDir = @ScriptDir & "\DataText_received_files"
               Local $windowtitle = "DataText_received_files"
               Run("explorer.exe " & $sDir);open explorer to see extracted files in folder
               Local $hExplorer = WinWait("[REGEXPTITLE:(?i)\Q" & $windowtitle & "\E$]")
               WinWaitClose($hExplorer)

               DirRemove(@ScriptDir&"\DataText_received_files",1); when files have been viewed, delete the DataText_received_files dir
               DirCreate(@ScriptDir&"\DataText_received_files");recreate the DataText_received_files dir

I wonder if there is a way to check both cases, so that the program works in either explorer setting?

Link to comment
Share on other sites

Change the regular expression to whatever you need. But have you tried the code you have there with both options? Because as far as I can tell, it should already work with both options as you have it there.

/edit: although if you were to navigate into another folder, the WinWaitClose thinks it's done (because there is no longer a window matching that title definition), and the script continues, even though your explorer window is actually still present (just with another folder in the title). That may not be what you want. One fix would be to remove the $ from your regex so that the regex, written like this at least, effectively changes to "contains" instead of "must end with". Then you could enter subfolders. But if you were to leave your folder (i.e. "go up one"), the WinWaitClose would still think it was done. Depending on what you plan to do in that explorer window, you may need a slightly more advanced approach.

Never mind all that, I'm a dummy. The current code already gets the windows handle and waits for that to close, so you can safely navigate.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Change the regular expression to whatever you need. But have you tried the code you have there with both options? Because as far as I can tell, it should already work with both options as you have it there.

​Yes, both code snippets work fine, but the first works if the explorer window title is @ScriptDir & "\DataText_received_files"

whereas the second one works if the explorer window title is "DataText_received_files".

This setting depends on the user (how he has set the explorer windows titles to appear).

You mention changing the regular expression, can this be used to include both case?

A bit of code would be appreciated.

Link to comment
Share on other sites

What I mean is that it looks like the last piece of code you posted should in fact already work in both modes. The first one (from jguinch) only works in full-path-mode.

The regex in "[REGEXPTITLE:(?i)\Q" & $windowtitle & "\E$]" means:
1) the title should end with whatever is in $windowtitle (because it's a string followed by $).

2) the match is to be done case-insensitively with (?i)

3) any special regex-specific characters in $windowtitle should be interpreted as literal characters (the \Q...\E)

As far as I can tell, that should match any window that ends with the folder name, no matter whether there is a path in front of it (or a Zen koan, or the entire King James bible).

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

What I mean is that it looks like the last piece of code you posted should in fact already work in both modes. The first one (from jguinch) only works in full-path-mode. 

​You are absolutely right!

It worked in both cases. A big thanks to all of you.

Link to comment
Share on other sites

neazoi : can you explain us what you are trying to automate with the explorer ? There are a lot of function for file/folder automation.

You can also try this one, which use a registry check to find if the explorer displays the full path or no. And I had REGEXPCLASS to work only with the explorer (I know CabinetWClass and ExploreWClass classes, but there are certainly others, depending of the OS version)

 

$sDir = "c:\temp"

$iFullPath = Number(RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState", "FullPath"))

Run("explorer.exe " & $sDir);open explorer to see extracted files in folder
$sTitle = $iFullPath ? $sDir : StringRegExpReplace($sDir, ".*\\", "")
Local $hExplorer = WinWait("[REGEXPCLASS:(?i)ExploreWClass|CabinetWClass; REGEXPTITLE:(?i)\Q" & $sTitle & "\E$]")
WinWaitClose($hExplorer)

 

Edited by jguinch
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...