Fly*_*wat 6 .net accurev process
我有一个可执行文件,它从命令提示符立即运行,但在使用System.Diagnostics.Process生成时似乎永远不会返回:
基本上,我正在围绕Accurev CLI接口编写.NET库包装器,因此每个方法调用都会生成CLI进程以执行命令.
这适用于除一个命令以外的所有命令:
accurev.exe show depots
Run Code Online (Sandbox Code Playgroud)
但是,当从控制台运行它时,它运行正常,当我使用.net进程调用它时,它会挂起...我使用的进程生成代码是:
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;
}
Run Code Online (Sandbox Code Playgroud)
它挂在p.WaitForExit().
有什么建议?
编辑:解决了!
如果输出缓冲区溢出,.NET进程挂起,我切换到使用异步读取方法,一切正常:
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();
}
}
Run Code Online (Sandbox Code Playgroud)