Since I started using AutoIt, I always thought the help file example for using StdOutRead and StdErrRead was the only way to read the StdOut and StdErr streams of a program being executed. I ran across an example from another user that showed that these streams do not need to be read in a loop while the program is running, but that they can be read even after the program has closed. I've modified the example script shown in the help file to show what I'm talking about.
; This is the example script from the help file slightly modified to use ConsoleWrite instead of a MsgBox
#include <Constants.au3>
Local $foo = Run(@ComSpec & " /c dir foo.bar", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
$line = StdoutRead($foo)
If @error Then ExitLoop
ConsoleWrite("STDOUT read: " & $line & @CRLF)
WEnd
While 1
$line = StderrRead($foo)
If @error Then ExitLoop
ConsoleWrite("STDERR read: " & $line & @CRLF)
WEnd
MsgBox(0, "Debug", "Exiting...")
; This is the same example of reading StdOut and StdErr but without using a loop.
; this example shows no blank lines in the output text.
Local $foo = Run(@ComSpec & " /c dir foo.bar", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
While ProcessExists($foo)
Sleep(10)
WEnd
$line = StdoutRead($foo)
ConsoleWrite("STDOUT read: " & $line & @CRLF)
$line = StderrRead($foo)
ConsoleWrite("STDERR read: " & $line & @CRLF)
The second example shows that these streams are still able to be read even after the Run command program has exited, in fact the way it's written, they're not read until it has exited. You'll also notice that the output from the second example doesn't have the numerous blank lines that the first example shows. While this method may not be the officially approved way of reading the streams, and won't work for any script that needs to read these in real time, it should be mentioned that it's possible to do it this way in the help file, because I never knew that you COULD read them this way.