Windows Vista Forums

StdOut.ReadAll blocking?

  1. #1


    lucadentella Guest

    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

  2. #2


    Old Pedant Guest

    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

  3. #3


    lucadentella Guest

    Re: StdOut.ReadAll blocking?

    Hello!

    Thanks! Your solution resolved my problem!


    > 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

StdOut.ReadAll blocking?

Similar Threads
Thread Forum
Redirection 1>StdOut.txt 2>StdErr.txt
Hi, I have some tasks that take hours to run. Using WSH I'm able to run the task in silent mode (scheduled task) sending the output to StdOut.txt...
PowerShell
Changes to stdout, stderr in PowerShell 2.0
How did PowerShell's handling of stdout and stderr with native commands change in v2.0? We have an application that intercepts stdout and stderr...
PowerShell
StdOut.ReadAll() in VBScript
I am writing a script to read user names from a txt file then run the calcs command on their application data folder in their home dir to check...
VB Script
StdOut and StdErr reading...
Hello! I'm working on a script to automate SFTP operations (using a text- based sftp client). My idea is to use the Exec() method to run the...
VB Script
redirecting stdin and stdout
Is there a way to redirect stdin and stdout of an executable using powershell? I need to have an input file and an output file open when this...
PowerShell
redirect powershell stdout to objShell.Exec.Stdout.ReadAll()(
Hi, I have a similar problem I am dealing with. Hence posting in this discussion chain. I have a powershell script text.ps1 And then I have...
PowerShell
stdout redirection
Hello everyone, I'm pretty new to powershell scripting so this might seem like a stupid question, excuse me for that. I already searched the forum...
PowerShell