Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had the same kind of problem that error was just hanging.</p> <p>Based on your response to Daniel Hilgarth I didn't even try using those codes though i think they would have worked for me.</p> <p>Since I want to be able do some fancier output still eventually i decided that I would do it with both of the outputs being done in a background thread.</p> <pre><code>public static class RunCommands { #region Outputs Property private static object _outputsLockObject; private static object OutputsLockObject { get { if (_outputsLockObject == null) Interlocked.CompareExchange(ref _outputsLockObject, new object(), null); return _outputsLockObject; } } private static Dictionary&lt;object, CommandOutput&gt; _outputs; private static Dictionary&lt;object, CommandOutput&gt; Outputs { get { if (_outputs != null) return _outputs; lock (OutputsLockObject) { _outputs = new Dictionary&lt;object, CommandOutput&gt;(); } return _outputs; } } #endregion public static string GetCommandOutputSimple(ProcessStartInfo info, bool returnErrorIfPopulated = true) { // Redirect the output stream of the child process. info.UseShellExecute = false; info.CreateNoWindow = true; info.RedirectStandardOutput = true; info.RedirectStandardError = true; var process = new Process(); process.StartInfo = info; process.ErrorDataReceived += ErrorDataHandler; process.OutputDataReceived += OutputDataHandler; var output = new CommandOutput(); Outputs.Add(process, output); process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); // Wait for the process to finish reading from error and output before it is finished process.WaitForExit(); Outputs.Remove(process); if (returnErrorIfPopulated &amp;&amp; (!String.IsNullOrWhiteSpace(output.Error))) { return output.Error.TrimEnd('\n'); } return output.Output.TrimEnd('\n'); } private static void ErrorDataHandler(object sendingProcess, DataReceivedEventArgs errLine) { if (errLine.Data == null) return; if (!Outputs.ContainsKey(sendingProcess)) return; var commandOutput = Outputs[sendingProcess]; commandOutput.Error = commandOutput.Error + errLine.Data + "\n"; } private static void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outputLine) { if (outputLine.Data == null) return; if (!Outputs.ContainsKey(sendingProcess)) return; var commandOutput = Outputs[sendingProcess]; commandOutput.Output = commandOutput.Output + outputLine.Data + "\n"; } } public class CommandOutput { public string Error { get; set; } public string Output { get; set; } public CommandOutput() { Error = ""; Output = ""; } } </code></pre> <p>This worked for me and allowed me to not have to use a timeout for the read.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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