• <small id='IonsW'></small><noframes id='IonsW'>

    <legend id='IonsW'><style id='IonsW'><dir id='IonsW'><q id='IonsW'></q></dir></style></legend>
  • <i id='IonsW'><tr id='IonsW'><dt id='IonsW'><q id='IonsW'><span id='IonsW'><b id='IonsW'><form id='IonsW'><ins id='IonsW'></ins><ul id='IonsW'></ul><sub id='IonsW'></sub></form><legend id='IonsW'></legend><bdo id='IonsW'><pre id='IonsW'><center id='IonsW'></center></pre></bdo></b><th id='IonsW'></th></span></q></dt></tr></i><div id='IonsW'><tfoot id='IonsW'></tfoot><dl id='IonsW'><fieldset id='IonsW'></fieldset></dl></div>

      1. <tfoot id='IonsW'></tfoot>
          <bdo id='IonsW'></bdo><ul id='IonsW'></ul>

        通过 C# 的 Process 类生成时,处理标准错误和程序输出的正确方法?

        时间:2023-11-10

              <bdo id='gSwoh'></bdo><ul id='gSwoh'></ul>

              <legend id='gSwoh'><style id='gSwoh'><dir id='gSwoh'><q id='gSwoh'></q></dir></style></legend>
                  <tbody id='gSwoh'></tbody>

              • <small id='gSwoh'></small><noframes id='gSwoh'>

              • <tfoot id='gSwoh'></tfoot>
                <i id='gSwoh'><tr id='gSwoh'><dt id='gSwoh'><q id='gSwoh'><span id='gSwoh'><b id='gSwoh'><form id='gSwoh'><ins id='gSwoh'></ins><ul id='gSwoh'></ul><sub id='gSwoh'></sub></form><legend id='gSwoh'></legend><bdo id='gSwoh'><pre id='gSwoh'><center id='gSwoh'></center></pre></bdo></b><th id='gSwoh'></th></span></q></dt></tr></i><div id='gSwoh'><tfoot id='gSwoh'></tfoot><dl id='gSwoh'><fieldset id='gSwoh'></fieldset></dl></div>
                • 本文介绍了通过 C# 的 Process 类生成时,处理标准错误和程序输出的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我阅读了 Process.StandardOutput 的文档,其中有这样的引用:

                  I read the documentation for Process.StandardOutput, which has this quote:

                  如果父进程在 p.StandardOutput.ReadToEnd 之前调用 p.WaitForExit 并且子进程写入足够的文本来填充重定向的流,则可能导致死锁情况.

                  A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream.

                  所以我想知道.如果我还担心在某些情况下可能会填充 StandardError,那么正确的方法是什么?

                  So I'm wondering. What is the correct way to do this if I'm also afraid that StandardError could be filled in some scenarios?

                  我是否必须使用循环来交替读取标准输出和错误,以避免填满,或者这个简单的代码是否足够:

                  Do I have to use a loop to alternate reading from standard output and error, to avoid either filling up, or is this simple code enough:

                  string error = proc.StandardError.ReadToEnd();
                  string output = proc.StandardOutput.ReadToEnd();
                  bool didFinish = proc.WaitForExit(60000);
                  

                  <小时>

                  在发布一些答案后编辑

                  所以这是正确的方法?

                  var output = new StringBuilder();
                  proc.OutputDataReceived += (s, e) => output.Append(e.Data);
                  proc.BeginOutputReadLine();
                  string error = proc.StandardError.ReadToEnd();
                  bool didFinish = proc.WaitForExit(60000);
                  

                  然后,我仅在过程实际完成时才使用字符串生成器内容.

                  And then I use the stringbuilder content only if the process actually finished.

                  那是正确的方法吗?

                  推荐答案

                  您的示例代码可能会导致死锁情况,其中有一些内容写入 StandardOutput 而不是 StandardError.您链接的文档中的下一个示例说明了这一点.

                  Your example code could cause a deadlock situtation where there was something written to the StandardOutput and not to StandardError. The very next example from the documentation you linked states as much.

                  基本上,我建议的是,在写入流时使用两个流上的异步读取来填充缓冲区,然后调用 WaitForExit.

                  Essentially, what I would recommend, is using the async reads on both streams to fill a buffer as the Streams are written to, and then call WaitForExit.

                  这篇关于通过 C# 的 Process 类生成时,处理标准错误和程序输出的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SerialPort 数据丢失 - C# 下一篇:Jenkins REST API 获取作业和作业控制台日志

                  相关文章

                  <legend id='LXOyo'><style id='LXOyo'><dir id='LXOyo'><q id='LXOyo'></q></dir></style></legend>

                    1. <small id='LXOyo'></small><noframes id='LXOyo'>

                      <i id='LXOyo'><tr id='LXOyo'><dt id='LXOyo'><q id='LXOyo'><span id='LXOyo'><b id='LXOyo'><form id='LXOyo'><ins id='LXOyo'></ins><ul id='LXOyo'></ul><sub id='LXOyo'></sub></form><legend id='LXOyo'></legend><bdo id='LXOyo'><pre id='LXOyo'><center id='LXOyo'></center></pre></bdo></b><th id='LXOyo'></th></span></q></dt></tr></i><div id='LXOyo'><tfoot id='LXOyo'></tfoot><dl id='LXOyo'><fieldset id='LXOyo'></fieldset></dl></div>
                        <bdo id='LXOyo'></bdo><ul id='LXOyo'></ul>

                    2. <tfoot id='LXOyo'></tfoot>