C# cmd output in real time

感情迁移 提交于 2019-12-12 02:34:06

问题


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

回答1:


If you want to get the output without waiting use the async features. Checkout http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx

Set up eventhandler

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

start async read of standard output

proc.BeginOutputReadLine();

I have modified your code to use these features.

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



回答2:


I sugguest having a look on the Backgroundworker class MSDN on Backgroundworker

Here you have a simple callback mechanism to present progress, you just have to provide a handler that does the progress updates.

From MSDN:

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

With the

public void ReportProgress(
int percentProgress,
Object userState)

function you can give back any state changes that you want to, especially the dynamic progress you might need (what is going on in your worker).

But I have to admit that I'm not sure about your "real time" part.



来源:https://stackoverflow.com/questions/11273409/c-sharp-cmd-output-in-real-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!