.NET Spawning Issues

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(); // 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 outputData.ToString(); } } 
+6
process accurev
source share
3 answers

Is he looking for entry? In particular, I notice that you are redirecting stdin, but not closing it, so if it reads stdin, it will hang.

+2
source share

Is the spawning process still alive while WaitForExit () is executing? Can you connect a debugger to it?

+1
source share

try replacing WaitForExit () with something like this:

 while (!p.WaitForExit(100)) Console.Write("."); 

Another thing to try is to set UseShellExecute to true and see how the console window opens. See this page for the intricacies of this parameter: http://blogs.msdn.com/jmstall/archive/2006/09/28/CreateNoWindow.aspx

-2
source share

All Articles