C#cmd实时输出

C#cmd实时输出,c#,cmd,real-time,C#,Cmd,Real Time,我正在编写一个使用隐藏控制台CMD的程序。在编解码器“ffmpeg”中启动。每次转换时都会收到反馈 我希望每1秒从控制台充电一次。不幸的是,互联网上所有可用的代码都是按照转换后退款的原则工作的,我想一直关注你发生的事情 public void ExecuteCommandSync(object command) { try { // create the ProcessStartInfo using "cmd" as the program to be r

我正在编写一个使用隐藏控制台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
      }
}

我建议你看看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)
函数,您可以返回您想要的任何状态更改,尤其是 您可能需要的动态进度(您的员工正在发生的事情)


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

如果您希望在不等待的情况下获得输出,请使用异步功能。 结帐

设置事件处理程序

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

这是谷歌翻译公司制作的吗?“退款”部分非常可疑。这里有个好提示:))谢谢,但这并没有解决我的问题。我仍然不知道CMD中发生了什么。在代码中添加了WaitForExit,以防出现问题。你能进一步解释一下你的情况吗?