在我用 WPF 编写的代码中,我在 FFmpeg 中运行了一些过滤器,如果我在终端(PowerShell 或 cmd 提示符)中运行命令,它将逐行给我信息。
我从 C# 代码调用该过程,它工作正常。我的代码存在的问题实际上是我无法从我运行的进程中获得任何输出。
我已经为 FFmpeg 进程尝试了 StackOverflow 的一些答案。我在我的代码中看到了 2 个机会。我可以通过 Timer 方法修复它,或者将事件挂钩到 OutputDataReceived。
我尝试了 OutputDataReceived 事件,但我的代码从未成功。我尝试了 Timer Approach 但仍然没有达到我的代码。请检查下面的代码
_process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = ffmpeg,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};
_process.OutputDataReceived += Proc_OutputDataReceived;
_process.Exited += (a, b) =>
{
System.Threading.Tasks.Task.Run(() =>
{
System.Threading.Tasks.Task.Delay(5000);
System.IO.File.Delete(newName);
});
//System.IO.File.Delete()
};
_process.Start();
_timer = new Timer();
_timer.Interval = 500;
_timer.Start();
_timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
while (_process.StandardOutput.EndOfStream)
{
string line = _process.StandardOutput.ReadLine();
}
// Check the process.
}