Sign in to follow this  
Followers 0
Adams100

StdoutRead() question

4 posts in this topic

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.

Share this post


Link to post
Share on other sites



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

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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.


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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0