ProcessInfo and RedirectStandardOutput

前端 未结 6 1149
無奈伤痛
無奈伤痛 2020-11-27 16:54

I have an app which calls another process in a command window and that process has updating stats that output to the console window. I thought this was a fairly simple opera

6条回答
  •  青春惊慌失措
    2020-11-27 17:20

    Interestingly you can't read from standard output and standard error at the same time:

    if you redirect both standard output and standard error and then try to read both, for example using the following C# code.

    [C#]

    string output = p.StandardOutput.ReadToEnd();

    string error = p.StandardError.ReadToEnd();

    p.WaitForExit();

    In this case, if the child process writes any text to standard error it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.

    http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.71).aspx

提交回复
热议问题