Author |
|
onhiatus Senior Member
Joined: May 12 2004 Location: United States
Online Status: Offline Posts: 279
|
Posted: October 19 2006 at 03:55 | IP Logged
|
|
|
I'm trying to get a macro to run an exe and then wait for it to finish.
The only think I've been able to come up with is to run the exe in the first line of the batch file then have the 2nd line use PHWMCmd to set a powehome variable.
The macro would then launch the batch file and loop waiting for the variable to get set.
Surly there has got to be a better way?
Actually all I really want is the output of the exe, but the only way I've been able to get this is to redirect the output to a file then read the file. So I need some way to tell when the exe is done and the file can be read.
Suggestions?
|
Back to Top |
|
|
cmhardwick Senior Member
Joined: July 08 2006 Location: United States
Online Status: Offline Posts: 290
|
Posted: October 19 2006 at 09:16 | IP Logged
|
|
|
Is there a way to set PH to look for the existence of the file you redirect to? Of course, each time you start the exe, you'd need to delete the file. I know with batch files, you can do "if exist" or something similar to see if the file is there. Not sure if there is a similar function built into PH.
__________________ Cicero, Enjoying automation!
|
Back to Top |
|
|
Manny Senior Member
Joined: March 23 2003 Location: United States
Online Status: Offline Posts: 172
|
Posted: October 19 2006 at 12:51 | IP Logged
|
|
|
Would the following help?
ph_fileexists PowerHome formula function
Description
Checks whether the specified file exists or not.
Syntax ph_fileexists ( s )
Argument Description
s
String representing the full path and filename of the file to check.
Return value
Integer. Returns a 1 if the file exists. Returns 0 if the file is not found.
|
Back to Top |
|
|
onhiatus Senior Member
Joined: May 12 2004 Location: United States
Online Status: Offline Posts: 279
|
Posted: October 19 2006 at 13:13 | IP Logged
|
|
|
The file may take several (up to 10) seconds to write, so it's not enought that the file exists.
Of course, I could use the batch file idea with something like:
1. Delete output file
2. Run exe with output directed to temp file
3. rename temp file to output file
Then I could use ph_fileexists to make the macro wait.
Not ideal, but it should work.
|
Back to Top |
|
|
Manny Senior Member
Joined: March 23 2003 Location: United States
Online Status: Offline Posts: 172
|
Posted: October 19 2006 at 13:26 | IP Logged
|
|
|
I was thinking maybe:
Your .exe writes 2 files, the one you want and a temporary "processing.txt" file that disappears once the the other is complete?
You then have an additional flag that lets ph_fileexists do its thing when the time is right.
Let us know when you have solved this, sounds useful.
...Still thinking out loud...
Maybe create a file called "proceed.txt" that gets renamed to "wait.txt" while processing, and renamed back to "proceed.txt" when ok.
Never mind my brain hurts...I was just bored at work, and it sounded like I good idea before I typed it.
Edited by Manny - October 19 2006 at 13:33
|
Back to Top |
|
|
onhiatus Senior Member
Joined: May 12 2004 Location: United States
Online Status: Offline Posts: 279
|
Posted: October 19 2006 at 13:42 | IP Logged
|
|
|
It's a little more complicated as I don't want various runs interfering with each other.
I think I'll have my macro pass in the name of the "flag" file, it can be a time stamp, or random, or??? That way multiple hits, or more than one macro using the mechanism won't colide.
As of right now I'm thinking the batch file is a more flexible way to handle this - at least until I find something that works.
This still seems clumsy. If ph_run returned a handle then it would be easier to wait (plus this seems useful for other interesting interprocess communications), or adding another function (ph_exec? ph_runwait?) that would launch the command and wait for it to finish.
Even better how about (ph_run_returnoutput - or some better name) that would run the command and return the standard output. Would be great for crawling directories, writting custom commands, parsing any OS commands, etc.
|
Back to Top |
|
|
dhoward Admin Group
Joined: June 29 2001 Location: United States
Online Status: Offline Posts: 4447
|
Posted: October 19 2006 at 14:23 | IP Logged
|
|
|
The next version has the new ph_runsync function. You specify the path/file to execute and a timeout (optionally infinite) to wait and PowerHome will run the application synchronously and will wait either for the timeout or until the application has terminated. The function also returns the exit code if the application does so.
Hope this helps,
Dave.
|
Back to Top |
|
|
onhiatus Senior Member
Joined: May 12 2004 Location: United States
Online Status: Offline Posts: 279
|
Posted: October 19 2006 at 15:36 | IP Logged
|
|
|
That would do exactly what I wanted. I know you hate answering this, but any guess on when the next version will be out?
|
Back to Top |
|
|
onhiatus Senior Member
Joined: May 12 2004 Location: United States
Online Status: Offline Posts: 279
|
Posted: February 19 2007 at 15:52 | IP Logged
|
|
|
WARNING: the following is typed from memory - I might be off on specific ph function names, or even my basic algorithm...
So I've been using the following method for awhile with intermittent failures:
1. PH macro picks a random # (1-1000)
3. PH macro deletes the file if it exists (eg. tmp789.txt)
4. PH macro launches an external batch file (see below)
5. PH macro enters a wait loop to wait for the above file to exist
6. PH macro reads said file
7. PH macro deletes file
The batch file:
1. Launch the app, redirecting output to specified file with different extension (e.g. tmp789.tmp)
2. Rename file (e.g. tmp789.tmp --> tmp789.txt)
At first I thought the failures might be collisions (two runs use the same random id), but it was happening to often. This last weekend I tracked it down. The problem was in the waitfor macro - I was using a wait command, and it turns out that PH returns from a waiting macro (called via ph_macroparam(macroname, "", "", "", "", "") at the wait - so ph_macroparam was returning while the wait loop continued to run.
At first I thought this was a bug, but it might be by design. Anyway it all works well now that I am using a Delay command instead - only problem is this can cause quite a backlog when it has to wait 10 seconds (or longer).
I'm considering rewriting the macro using global variables, but I'm almost guaranteed to get a collision at some point. Can anyone think of another way to do this that could use the Wait command?
Thanks, Tony
|
Back to Top |
|
|
Viper Groupie
Joined: January 14 2007 Location: United States
Online Status: Offline Posts: 88
|
Posted: February 19 2007 at 16:21 | IP Logged
|
|
|
You could set a global variable indicating a copy of the macro is waiting and other invocations of the macro would loop waiting for the global variable to be reset (or quit, I don't know your application) before proceeding and setting the global variable. Another technique I've used in a trigger macro is to disable the trigger until I've completed my "critical" section of macro code, and then reenable it. You may need to put some code into the STARTUP macro to ensure everything is primed properly.
|
Back to Top |
|
|
onhiatus Senior Member
Joined: May 12 2004 Location: United States
Online Status: Offline Posts: 279
|
Posted: February 20 2007 at 12:59 | IP Logged
|
|
|
Not quite what I'm after. Basically I have a remote hta (MS html application) that hosts a web page for controlling power home. Problem is that some actions need to launch an external application, wait for the output and then return the output to the hta. Some of the searches / processing can take 10 seconds - or more, others happen almost instantly.
Should not be too difficult, except that the call from the loading psp page (ph_macroparam) seems to returns as soon as it gets to the wait statement which means I have to use a delay statement which means that PH is locked up for up to 20+ seconds at a time.
|
Back to Top |
|
|