从Process.Start()获取所需的输出时出现问题 [英] Problems getting desired output from Process.Start()

查看:185
本文介绍了从Process.Start()获取所需的输出时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个应用程序,调用几个命令行应用程序对一些视频文件做一些后处理。



目前我尝试使用 Comskip a>从我的有线卡调谐器识别视频录制中的广告插播。这运行很好,但我有问题获得我需要的屏幕输出。

  String stdout = null; 

using(var process = new Process())
{
var start = new ProcessStartInfo(comskip,cmdLine);

start.WindowStyle = ProcessWindowStyle.Normal;
start.CreateNoWindow = true;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;

process.StartInfo = start;

process.Start();
process.WaitForExit();

stdout = process.StandardOutput.ReadToEnd();
}

我期待 stdout 来抓取屏幕上显示的内容,与手动启动应用程序(下面的屏幕截图)相同,后者是应用程序正在做什么的连续Feed,并且在输出中混合的是给出%进度的行我要使用更新进度栏





但是运行上面的代码只会给我:


命令行使用的是:
C:\Users\Chris\Google Drive\Tools\ComSkip\comskip.exeC:\Users\Chris\Desktop\ComSkip调整文件\Modern Family.wtv--ini = C:\Users\Chris\Desktop\ComSkip Tuning Files \comskip_ModernFamily.ini



设置ini文件复制到C:\Users\Chris\Desktop\ComSkip根据命令行调整文件\comskip_ModernFamily.ini
使用C:\Users\Chris\Desktop\ComSkip调整文件我也尝试重定向 StandardError 流。 并抓取 process.StandardError.ReadToEnd(); ,但是如果我运行这些选项,进程似乎挂起。



我缺少一些东西来捕获我想要的东西,或者这个应用程序的输出流是否可能被访问的其他地方?

解决方案

您必须设置以下内容:

  process.StartInfo.RedirectStandardOutput = true; 
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived + = new DataReceivedEventHandler(ReadOutput);
process.ErrorDataReceived + = new DataReceivedEventHandler(ErrorOutput);

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(); code>

并捕获 ReadOutput code> ErrorOutput

  private static void ErrorOutput(object sender,DataReceivedEventArgs e)
{
if(e.Data!= null)
{
stdout =Error:+ e.Data;
}
}

private static void ReadOutput(object sender,DataReceivedEventArgs e)
{
if(e.Data!= null)
{
stdout = e.Data;
}
}


I am working on an application that calls several command line applications to do some post processing on some video files.

Right now I am trying to use Comskip to identify the commercial breaks in a video recording from my cable card tuner. This runs just fine, but I am having problems getting the screen output that I need.

String stdout = null;

using (var process = new Process())
{
    var start = new ProcessStartInfo(comskip, cmdLine);

    start.WindowStyle = ProcessWindowStyle.Normal;
    start.CreateNoWindow = true;
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;

    process.StartInfo = start;

    process.Start();
    process.WaitForExit();

    stdout = process.StandardOutput.ReadToEnd();
}

I'm expecting stdout to grab what is displayed on the screen the same as when the application is launched manually (screen shot below) which is a continuous feed of what the application is doing, and mixed in the output are lines that give a % progress, which I want to use to update a progress bar

But running the above code only gives me:

The commandline used was: "C:\Users\Chris\Google Drive\Tools\ComSkip\comskip.exe" "C:\Users\Chris\Desktop\ComSkip Tuning Files\Modern Family.wtv" "--ini=C:\Users\Chris\Desktop\ComSkip Tuning Files\comskip_ModernFamily.ini"

Setting ini file to C:\Users\Chris\Desktop\ComSkip Tuning Files\comskip_ModernFamily.ini as per commandline Using C:\Users\Chris\Desktop\ComSkip Tuning Files\comskip_ModernFamily.ini for initiation values.

I also tried redirecting the StandardError stream and grabbing process.StandardError.ReadToEnd(); but the process appears to hang if I run with these options.

Am I missing something to capture what I'm hoping for, or is it possible that the output stream for this application is going somewhere else that is not accessible?

解决方案

You must set following:

     process.StartInfo.RedirectStandardOutput = true;
     process.StartInfo.RedirectStandardError = true;
     process.StartInfo.UseShellExecute = false;
     process.OutputDataReceived += new DataReceivedEventHandler(ReadOutput);
     process.ErrorDataReceived += new DataReceivedEventHandler(ErrorOutput);

     process.Start();
     process.BeginOutputReadLine();
     process.BeginErrorReadLine();
     process.WaitForExit();

and catch the output in ReadOutput and ErrorOutput

  private static void ErrorOutput(object sender, DataReceivedEventArgs e)
  {
     if (e.Data != null)
     {
        stdout = "Error: " + e.Data;
     }
  }

  private static void ReadOutput(object sender, DataReceivedEventArgs e)
  {
     if (e.Data != null)
     {
        stdout = e.Data;
     }
  }

这篇关于从Process.Start()获取所需的输出时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆