Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - StdOut.ReadAll blocking?

Reply
 
Old 07-11-2008   #1 (permalink)
lucadentella


 
 

StdOut.ReadAll blocking?

Hello!

I'm working on a script using the WshShell.Exec() method.
To avoid buffer overflow problem (if I read the whole StdOut at the
end of the execution, sometimes my script hangs) I found a suggestion
on the "Hey, Scripting Guy!" MS blog: use a StdOut.ReadAll inside the
waiting loop.

But please notice these two examples:

-- Ex1, without ReadAll --

Dim elapsedSecs
elapsedSecs = 0

Dim quit
quit = False

Dim objShell
Set objShell = CreateObject("WScript.Shell")

Dim objCmd
Set objCmd = objShell.Exec("ping 10.113.203.200")

Do While quit = False

If objCmd.Status = 1 Then
quit = True
Else
WScript.Sleep 100
elapsedSecs = elapsedSecs + 0.1
End If


Loop

WScript.Echo "Elapsed seconds: " & elapsedSecs

-- output: "Elapsed seconds: 2,8"


-- Ex2, with ReadAll --

Dim elapsedSecs
elapsedSecs = 0

Dim quit
quit = False

Dim objShell
Set objShell = CreateObject("WScript.Shell")

Dim objCmd
Set objCmd = objShell.Exec("ping 10.113.203.200")

Do While quit = False

If Not objCmd.StdOut.AtEndOfStream Then
outBuffer = outBuffer & objCmd.StdOut.ReadAll
End If

If objCmd.Status = 1 Then
quit = True
Else
WScript.Sleep 100
elapsedSecs = elapsedSecs + 0.1
End If


Loop

WScript.Echo "Elapsed seconds: " & elapsedSecs

-- output: "Elapsed seconds: 0"


It seems ReadAll is blocking my loop until the objCmd exits... why?
thanks!

My System SpecsSystem Spec
Old 07-11-2008   #2 (permalink)
Old Pedant


 
 

RE: StdOut.ReadAll blocking?

ReadAll does a *synchronous* read.

It really and truly means "read all output produced by writes to StdOut by
the given object."

So of course it has to wait until the logical EOF is received from the thing
it is reading from. And that doesn't happen with PING until the ping object
is done.

How about trying ReadLine???

startTime = Timer( )
quit = False
outBuffer = ""
Set objShell = CreateObject("WScript.Shell")
Set objCmd = objShell.Exec("ping 10.113.203.200")
Do
If Not objCmd.StdOut.AtEndOfStream Then
outBuffer = outBuffer & objCmd.StdOut.ReadLine
End If
If objCmd.Status = 1 Then Exit Do
If timer( ) > startTime + 20 Then
outBuffer = outBuffer & vbNewLine & "*** TIMED OUT ***"
Exit Do
End If
WScript.Sleep 100
Loop
endTime = Timer( )
WScript.Echo "Elapsed seconds: " & (endTime - startTime)
WScript.Echo outBuffer



My System SpecsSystem Spec
Old 07-14-2008   #3 (permalink)
lucadentella


 
 

Re: StdOut.ReadAll blocking?

Hello!

Thanks! Your solution resolved my problem!

Quote:

> ReadAll does a *synchronous* read. *
>
> It really and truly means "read all output produced by writes to StdOut by
> the given object."
>
> So of course it has to wait until the logical EOF is received from the thing
> it is reading from. *And that doesn't happen with PING until the ping object
> is done.
>
> How about trying ReadLine???
>
> startTime = Timer( )
> quit = False
> outBuffer = ""
> Set objShell = CreateObject("WScript.Shell")
> Set objCmd = objShell.Exec("ping 10.113.203.200")
> Do
> * * If Not objCmd.StdOut.AtEndOfStream Then
> * * * * *outBuffer = outBuffer & objCmd.StdOut.ReadLine
> * * End If
> * * If objCmd.Status = 1 Then Exit Do
> * * If timer( ) > startTime + 20 Then
> * * * * *outBuffer = outBuffer & vbNewLine & "*** TIMED OUT ***"
> * * * * *Exit Do
> * * End If
> * * WScript.Sleep 100
> Loop
> endTime = Timer( )
> WScript.Echo "Elapsed seconds: " & (endTime - startTime)
> WScript.Echo outBuffer
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
StdOut.ReadAll() in VBScript VB Script
StdOut and StdErr reading... VB Script
redirect powershell stdout to objShell.Exec.Stdout.ReadAll()( PowerShell
stdout redirection PowerShell
Catching stdout AND stderr PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46