C#cmd实时输出

时间:2012-06-30 10:38:01

标签: c# cmd real-time

我正在编写一个使用隐藏控制台CMD的程序。从编解码器“ffmpeg”开始。当转换所有时间得到反馈。

我希望控制台每1秒充电一次。不幸的是,互联网上提供的所有代码都遵循转换后获得退款的原则,我希望一直关注您所发生的事情。


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
      }
} 

2 个答案:

答案 0 :(得分:1)

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

设置eventhandler

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
        }
    }

答案 1 :(得分:0)

我建议您查看Backgroundworker类MSDN on Backgroundworker

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

来自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)

功能,您可以回馈您想要的任何状态更改,尤其是 你可能需要的动态进展(你工人的动态)。

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