I have an executable that runs instantly from the command line, but it seems to never return when created using System.Diagnostics.Process:
I basically write a wrapper for a .NET library around the Accurev CLI, so every method call spawns a CLI process to execute the command.
This works great for everyone but one command:
accurev.exe show depots
However, when running this from the console, it works fine, when I call it using the .net process, it freezes ... The spawning code I use is:
public static string ExecuteCommand(string command) { Process p = createProcess(command); p.Start(); p.WaitForExit(); // Accurev writes to the error stream if ExitCode is non zero. if (p.ExitCode != 0) { string error = p.StandardError.ReadToEnd(); Log.Write(command + " failed..." + error); throw new AccurevException(error); } else { return p.StandardOutput.ReadToEnd(); } } /// Creates Accurev Process /// </summary> /// <param name="command"></param> /// <returns></returns> private static Process createProcess(string command) { Log.Write("Executing Command: " + command); ProcessStartInfo startInfo = new ProcessStartInfo(); Process p = new Process(); startInfo.CreateNoWindow = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; startInfo.Arguments = command; startInfo.FileName = _accurev; p.StartInfo = startInfo; return p; }
It hangs on p.WaitForExit ().
Any tips?
EDIT : solved!
.NET The process freezes if the output buffer is full, I switched to using the asynchronous read method and everything works:
public static string ExecuteCommand(string command) { StringBuilder outputData = new StringBuilder(); Process p = createProcess(command); p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { outputData.AppendLine(e.Data); }; p.Start(); p.BeginOutputReadLine(); p.WaitForExit();
Flyswat
source share