Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that if you redirect <code>StandardOutput</code> and/or <code>StandardError</code> the internal buffer can become full. Whatever order you use, there can be a problem:</p> <ul> <li>If you wait for the process to exit before reading <code>StandardOutput</code> the process can block trying to write to it, so the process never ends.</li> <li>If you read from <code>StandardOutput</code> using ReadToEnd then <em>your</em> process can block if the process never closes <code>StandardOutput</code> (for example if it never terminates, or if it is blocked writing to <code>StandardError</code>).</li> </ul> <p>The solution is to use asynchronous reads to ensure that the buffer doesn't get full. To avoid any deadlocks and collect up all output from both <code>StandardOutput</code> and <code>StandardError</code> you can do this:</p> <p>EDIT: See answers below for how avoid an <strong>ObjectDisposedException</strong> if the timeout occurs.</p> <pre><code>using (Process process = new Process()) { process.StartInfo.FileName = filename; process.StartInfo.Arguments = arguments; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; StringBuilder output = new StringBuilder(); StringBuilder error = new StringBuilder(); using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false)) using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false)) { process.OutputDataReceived += (sender, e) =&gt; { if (e.Data == null) { outputWaitHandle.Set(); } else { output.AppendLine(e.Data); } }; process.ErrorDataReceived += (sender, e) =&gt; { if (e.Data == null) { errorWaitHandle.Set(); } else { error.AppendLine(e.Data); } }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (process.WaitForExit(timeout) &amp;&amp; outputWaitHandle.WaitOne(timeout) &amp;&amp; errorWaitHandle.WaitOne(timeout)) { // Process completed. Check process.ExitCode here. } else { // Timed out. } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload