1

I am writing a program that uses a hidden console CMD. Starts in the codec "ffmpeg". When converting all the time gets feedback.

I wish every 1 second charge results from the console. Unfortunately, all the codes available on the internet work on the principle that gets refund after the conversion and I want to be watching you all the time what was happening.


public void ExecuteCommandSync(object command)
{
     try
     {
         // create the ProcessStartInfo using "cmd" as the program to be run,
         // and "/c " as the parameters.
         // Incidentally, /c tells cmd that we want it to execute the command that follows,
         // and then exit.
    System.Diagnostics.ProcessStartInfo procStartInfo =
        new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    // The following commands are needed to redirect the standard output.
    // This means that it will be redirected to the Process.StandardOutput StreamReader.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    // Do not create the black window.
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    // Get the output into a string
    string result = proc.StandardOutput.ReadToEnd();
    // Display the command output.
    Console.WriteLine(result);
      }
      catch (Exception objException)
      {
      // Log the exception
      }
} 
4

2 回答 2

4

如果您想在不等待的情况下获得输出,请使用异步功能。结帐http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx

设置事件处理程序

proc.OutputDataReceived += (s,e) => { Console.WriteLine(e.Data);};

开始异步读取标准输出

proc.BeginOutputReadLine();

我已修改您的代码以使用这些功能。

    public static void ExecuteCommandSync(object command)
    {
        try
        {
            // create the ProcessStartInfo using "cmd" as the program to be run,
            // and "/c " as the parameters.
            // Incidentally, /c tells cmd that we want it to execute the command that follows,
            // and then exit.
            var procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);


            // The following commands are needed to redirect the standard output.
            // This means that it will be redirected to the Process.StandardOutput StreamReader.
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            // Now we create a process, assign its ProcessStartInfo and start it
            var proc = new System.Diagnostics.Process();
            proc.OutputDataReceived += (s,e) => { Console.WriteLine(e.Data);};
            proc.StartInfo = procStartInfo;
            proc.Start();
            proc.BeginOutputReadLine();
            proc.WaitForExit();
        }
        catch (Exception objException)
        {
            Console.WriteLine("Error: " + objException.Message);
            // Log the exception
        }
    }
于 2012-06-30T11:40:31.023 回答
0

我建议在 Backgroundworker 上查看 Backgroundworker 类MSDN

在这里,您有一个简单的回调机制来呈现进度,您只需要提供一个处理进度更新的处理程序。

来自 MSDN:

// This event handler updates the progress bar.
private void backgroundWorker1_ProgressChanged(object sender,
    ProgressChangedEventArgs e)
{
    this.progressBar1.Value = e.ProgressPercentage;
}

随着

public void ReportProgress(
int percentProgress,
Object userState)

您可以返回任何您想要的状态更改功能,尤其是您可能需要的动态进度(您的工作人员中发生了什么)。

但我不得不承认我不确定你的“实时”部分。

于 2012-06-30T11:17:18.397 回答