Jump to content

StdoutRead() question


Recommended Posts

Hi.

Can somebody help me understand the benefit of one over the other regarding these two ways of getting the StdoutRead()

#include <Constants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>

; display a list of files in C:\

Local $iPID = Run(@ComSpec & ' /C DIR C:\ /B', 'C:\', @SW_HIDE, $STDOUT_CHILD)

ProcessWaitClose($iPID)

Local $sOutput = StdoutRead($iPID)

Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)

_ArrayDisplay($aArray)

-

#include <Constants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>

; display a list of files in C:\

Local $iPID = Run(@ComSpec & ' /C DIR C:\ /B', 'C:\', @SW_HIDE, $STDOUT_CHILD)

Local $sOutput = ""

While 1
   $sOutput &= StdoutRead($iPID)
   If @error Then
      ExitLoop
   EndIf
WEnd

Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)

_ArrayDisplay($aArray)

The first one gets the StdoutRead after the process closes. The second one gets the StdoutRead in a while loop while the process is running. But is there a benefit to either one or is the end result the same every time?

Thanks.

Link to comment
Share on other sites

  • Developers

Only benefit I see is that you could show the STDOUT in the second example as it happens, like AutoIt3Wrapper does.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I think that first way of reading data from StdoutRead after the process has closed is not a good idea.

In some situations you may lose part of output.
>here an example of the issue

also,
as mr. Jos stated in previous post, using second way gives you the advantage of get StdOut data while is generated,

without to be forced to wait the end of command execution.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Cool. Thanks guys.

For my current needs I would have to wait for the process to end in order to get the output I want. So I figured if there was no other benefit in using the While loop besides being able read it during the execution, then I'd be better off using the ProcessWaitClose method because doing StdoutRead() in a While loop causes CPU usage to go through the roof on my PC when the process needs to run for more then a few seconds, and sometimes even minutes/hours depending on the situation so it's not really ideal.

But this is a pretty weird problem with ProcessWaitClose that Chimp pointed out. I wonder what causes it. And has this occurred in any other cases or was it just with PowerCfg? Ahh, this makes me want to go back to the While loop now hehe.

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