【问题标题】:C# cmd output in real timeC# cmd 实时输出
【发布时间】:2012-06-30 10:38:00
【问题描述】:

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

【问题讨论】:

  • 这是谷歌翻译制作的吗? “退款”部分非常可疑。
  • 小费在这里 :))

标签: c# cmd real-time


【解决方案1】:

如果您想在不等待的情况下获得输出,请使用异步功能。 结帐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
        }
    }

【讨论】:

  • 谢谢,但这并没有解决我的问题。我仍然无法忍受 CMD 中发生的事情。
  • 在代码中添加了 WaitForExit 以防出现问题。能否请您进一步说明您的情况?
【解决方案2】:

我建议看看 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)

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多