Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution
- From: don.jscript.usenet@xxxxxxxxx
- Date: Tue, 29 Jan 2008 07:44:10 -0800 (PST)
For those interested, my solution to the problem was complicated.
1. Execute the app.
2. Execute the monitor.
3. Read output from the app.
4. Read output from the monitor.
The monitor is another script. It has two parameters: the process ID
of the app to monitor, and the timeout in seconds. It uses WMI to
watch the specified process. If the process terminates, the monitor
ends. If the timeout elapses, the monitor terminates the process and
ends.
The next two posts will be the final code:
1. ExecWithTimeout.wsf: the main script.
2. ProcessTimeout.wsf: the monitor.
I'm sure this would have been much simpler in a compiled language or
dotNET. But I don't have that expertise or time to devote to learning
it.
On Jan 11, 10:21 am, don.jscript.use...@xxxxxxxxx wrote:
I want to execute an app, process its output, but terminate the app if
it takes too long. I thought using WScript.Shell.Exec() would be the
ticket. However, I'm finding that AtEndOfStream waits until text
appears in the executed app's stdout, or the app terminates. Read()
works the same, and ReadAll() waits for the app to terminate, though I
expect that behavior.
Is there a workaround or alternative? As it stands, I can either
process the app's output as it comes, or terminate the app if it takes
too long, but not both.
Here's a simple example. output.js pauses before and after outputting
a single character. process.js executes output.js, checks
AtEndOfStream, Read()'s the single character, and checks AtEndOfStream
again. You'll notice the pause before each AtEndOfStream is output --
AtEndOfStream waits for the single character, and then waits for the
app to terminate.
process.js
========================================
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("cscript /nologo output.js");
var ch;
var aeos;
WScript.StdOut.WriteLine("starting...");
aeos = oExec.StdOut.AtEndOfStream;
WScript.StdOut.WriteLine("AtEndOfStream=" + aeos);
ch = oExec.StdOut.Read(1);
WScript.StdOut.WriteLine("Read(1)=" + ch);
aeos = oExec.StdOut.AtEndOfStream;
WScript.StdOut.WriteLine("AtEndOfStream=" + aeos);
output.js
========================================
WScript.Sleep(3000);
WScript.StdOut.Write("a");
WScript.Sleep(3000);
.
- Follow-Ups:
- Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution - ProcessTimeout.wsf
- From: don . jscript . usenet
- Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution - ExecWithTimeout.wsf
- From: don . jscript . usenet
- Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution - ProcessTimeout.wsf
- References:
- WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- From: don . jscript . usenet
- WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- Prev by Date: Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- Next by Date: Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution - ExecWithTimeout.wsf
- Previous by thread: Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text
- Next by thread: Re: WScript.Shell.Exec().StdOut.AtEndOfStream waits for text - solution - ExecWithTimeout.wsf
- Index(es):
Relevant Pages
|