在CMD进程上调用的命令的读取错误输出挂起 [英] Reading error output hangs for command invoked on CMD process
问题描述
我有c#控制台应用程序的ffmpeg命令调用。这是它
I have c# console app for ffmpeg command invocation. Here is it
class Program
{
static void Main(string[] args)
{
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process console = Process.Start(cmd);
console.StandardInput.WriteLine(@"cd C:\Users\vishnu.aravind");
console.StandardInput.WriteLine(@"ffmpeg -i sunp.avi -i Alarm03.wav -c:v copy -c:a aac -strict experimental output.avi");
string errors = console.StandardError.ReadToEnd();
Console.WriteLine("Video file is created.");
Console.Read();
}
}
如果我删除代码行
if I remove the line of code
string errors = console.StandardError.ReadToEnd();
此程序将正常工作。否则它挂起。
this program will work fine. Else it hangs. I need the error information if any, what to do, please help.
推荐答案
问题的原因
您的程序挂起,因为
Cause of a problem
Your program hangs because
- cmd 是一个交互式命令行应用程序,从而持续产生 std的输出。 out 和 std。错误流
-
Process.StandardError.ReadToEnd()
读取 std的输出。错误流到达流结尾
- cmd is an interactive command line application and thus continually produces output for std. out and std. error stream
Process.StandardError.ReadToEnd()
reads the output of std. error stream of a process till it reaches end of stream
作为 cmd 命令行应用程序 - 它不会结束它的 std。错误流直到 cmd 终止 - 这就是为什么 Process.StandardError.ReadToEnd()
em> cmd 过程。
as cmd is an interactive command line application - it will not end it's std. error stream until process of cmd terminates - and that's why Process.StandardError.ReadToEnd()
hangs when it is invoked on running cmd process.
执行有至少两个以下选项:
To get output for each command you execute there are at least two following options:
- 使用单独的
Process
使用标准方法StreamReader
(ReadToEnd()
和读取)
方法)。 -
使用单个
进程
>应用程序 - 以交互方式读写它:
- Start each command with separate
Process
instance - and read output with standard methods ofStreamReader
class (ReadToEnd()
andRead()
methods). Use single
Process
instance for cmd application - to read and write to it interactively:
-
按照以下示例启动过程:
Start process as in following example:
Process interactiveProcess = new Process();
string processOutput = "";
interactiveProcess.StartInfo.FileName = this.processPath;
interactiveProcess.StartInfo.Arguments = commandLineParameters;
interactiveProcess.StartInfo.UseShellExecute = false;
interactiveProcess.StartInfo.CreateNoWindow = false;
interactiveProcess.StartInfo.RedirectStandardInput = true;
interactiveProcess.StartInfo.RedirectStandardError = true;
interactiveProcess.Start();
interactiveProcess.EnableRaisingEvents = true;
interactiveProcess.ErrorDataReceived += new DataReceivedEventHandler((process, outputEventArgs) => processOutput += outputEventArgs.Data);
interactiveProcess.BeginErrorReadLine();
使用以下代码写命令
Write commands to cmd with following code
interactiveProcess.StandardInput.WriteLine(command);
要读取响应,您需要使用 System.Threading。 Thread.Sleep()
方法并等待 processOutput
变量在执行启动命令期间填充输出。
To read response you will need to use System.Threading.Thread.Sleep()
method and wait till processOutput
variable is populated with output during execution of started commands.
这篇关于在CMD进程上调用的命令的读取错误输出挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!