What does Util.Cmd( ..., StreamResults: true) do?

Options

I have a script which calls an external program that takes a while and writes quite a few lines to console and I thought that this option would start displaying the output at once rather than waiting until the program had finished.

Later on when I was had more throughly debugged the script I change it not to dump the output from the cmd.

But this seems to have the effect of not actually running the command!.

An example

System.IO.File.Delete("testfile");

var output = Util.Cmd("echo hello > testfile", streamResults: true); 

System.IO.File.Exists("testfile").Dump();

output.Dump();
System.IO.File.Exists("testfile").Dump();
 

outputs

False
(0 items)
True

showing that the testfile is not created until after the output is dumped (or until I called something like output.ToArray())

Can anyone explain?

Comments

  • That's correct behavior. If streamResults is true, you get back a lazy IEnumerable with standard streaming/iterator/pipe semantics. This means that you can access results before the command has completed - and is potentially more efficient in that the results are not stored in memory - however you have to start enumerating the results in order to start the operation (also, the command may block if you don't enumerate the results fast enough).

  • Thanks.

    I think I have been exceedingly dense. I sat looking in ILSpy at the code

    public static IEnumerable Cmd(string commandText, string args, Encoding encoding, bool quiet = false, bool streamResults = false)
    {
        IEnumerable enumerable = CmdCore(commandText, args, quiet, encoding);
        if (!streamResults)
        {
            enumerable = enumerable.ToArray();
        }
        return new CmdResults(enumerable);
    }
    

    and thought, it doesn't actually pass streamResults to CmdCore, so how can it stop the process from starting?

    Completely overlooked the return value which makes all the difference!

    Doh!.