| 原文链接:https://www.cnblogs.com/sode/archive/2012/07/10/2583941.html 调用CMD执行命令将.h264转换成mp4, 结果到了Process的WaitForExit()方法就一直阻塞不动, 把程序退出后转换完成; 找了各种方法,直接输入exit就是扯淡,操作没完成就直接退出了; 真正的原因碰到这个问题的搬砖工应该都知道了: 以下死锁原因部分copy 由于标准输出流被重定向 p.StartInfo.RedirectStandardInput = true, 而Process.StandardOutput的缓冲大小是有限制的( 4k?),所以当缓冲满了的时候(执行上面的批处理文件有很多的输出),子进程(cmd.exe)会等待主进程(C# App)读取并释放此缓冲,而主进程由于调用了WaitForExit()方法,则会一进等待子进程退出,最后形成死锁 只要订阅处理错误消息就可以了,上代码 using (Process p = new Process()){
 p.StartInfo.FileName = "ffmpeg.exe";
 p.StartInfo.Arguments = convertAgrs;
 p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
 p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
 p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
 p.StartInfo.RedirectStandardError = true; //重定向标准错误输出
 p.StartInfo.CreateNoWindow = true; //不显示程序窗口
 p.ErrorDataReceived += new DataReceivedEventHandler(delegate (object sender, DataReceivedEventArgs e)
 {
 // DoSth.
 //Console.WriteLine("MP4转换error:" + e.Data);
 });
 p.OutputDataReceived += new DataReceivedEventHandler(delegate (object sender, DataReceivedEventArgs e)
 {
 // DoSth.
 //Console.WriteLine($"MP4转换信息{e.Data}");
 });
  p.Start();p.BeginErrorReadLine();
 p.BeginOutputReadLine();
 p.WaitForExit();//等待程序执行完退出进程
 p.Close();
  } |