问候stackoverflow成员,
在WPF Frontend的BackgroundWorker中我运行sox(开源控制台声音处理工具)System.Diagnostics.Process.以同样的方式,我使用其他几个命令行工具并解析他们的输出以在我的前端poulate进度条.
这适用于其他工具但不适用于Sox,因为它不是为每个进度步骤发送新行,而是通过仅使用回车符(\ r)和没有换行符(\n)更新控制台上的单行.我尝试了异步和同步读取process.StandardError.
使用异步process.ErrorDataReceived += (sender, args) => FadeAudioOutputHandler(clip, args);结合process.BeginErrorReadLine();不会产生任何的个人状态更新,因为某些原因,回车不触发的ReadLine,即使MSDN文档表明,它应该.当进程完成时,输出将在一个块中吐出.
然后,我通过流上的char读取尝试了以下代码用于同步char:
char[] c;
var line = new StringBuilder();
while (process.StandardError.Peek() > -1)
{
c = new char[1];
process.StandardError.Read(c, 0, c.Length);
if (c[0] == '\r')
{
var percentage = 0;
var regex = new Regex(@"%\s([^\s]+)");
var match = regex.Match(line.ToString());
if (match.Success)
{
myProgressObject.ProgressType = ProgressType.FadingAudio
//... some calculations omitted for brevity
percentage = (int) Math.Round(result);
}
else …Run Code Online (Sandbox Code Playgroud)