Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

.net - Passing through console output from Process.Start directly

I'm using Process.Start from a .NET command-line application to run another command-line application. I don't want to capture the output of the application, I just want it to go to the console directly.

With the program below the output seems to just disappear into thin air. If I leave CreateNoWindow at the default false then I do get the output in a fresh console window, but I want it in the original console window. UseShellExecute <- false is also needed otherwise CreateNoWindow is forced to false.

I could do something much more complicated using RedirectStandardOutput and RedirectStandardError and then capturing the output and re-printing it, but this is fiddly in combination with WaitForExit, particularly as in my real application I want to use the version that has a timeout.

Is there any way I can simply get the standard output and error passed through directly?

The behaviour I am seeing is confusing because the documentation for RedirectStandardOutput does seem to say clearly:

When a Process writes text to its standard stream, that text is typically displayed on the console.

Here's the demonstration code. When I run it with DummyRunner.exe I get the output from the first bit of code, and when I run it with DummyRunner.exe DummyRunner.exe I get nothing. Although the code is in F# there's nothing particularly F# specific about this problem that I know of.

module DummyRunner

open System
open System.Diagnostics

[<EntryPoint>]
let main args =

    // do something when called with no arguments, just so we can call
    // this with itself as an argument to make a self-contained test
    if args.Length = 0 then
        for n = 1 to 5 do
            printfn "Waiting %d" n
            System.Threading.Thread.Sleep(1000)
        System.Environment.Exit(0)

    let cmd = args.[0]
    let cmdArgs = args.[1..]

    let startInfo = ProcessStartInfo(cmd, String.Join(" ", cmdArgs))

    startInfo.UseShellExecute <- false
    startInfo.CreateNoWindow <- true

    let p = Process.Start(startInfo)

    p.WaitForExit()
    p.ExitCode
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not actually reading the standard output of your process. Redirecting the standard output adds the output to the process standard output, but does not read it into your running process.

let startInfo = ProcessStartInfo(cmd, String.Join(" ", cmdArgs))

startInfo.UseShellExecute <- false
startInfo.RedirectStandardOutput <- true;
let p = Process.Start(startInfo)

printfn p.StandardOutput.ReadToEnd();
p.WaitForExit()
p.ExitCode

this should forward the output from the running process to the parent running thread. See this page for more information on this.

redirect std output


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...