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
      }
} 
2
  • 5
    Was this produced by Google translate? The "refund" part is highly suspicious. Commented Jun 30, 2012 at 10:40
  • fine tip up here :)) Commented Dec 3, 2019 at 9:07

2 Answers 2

4

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
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but this did not solve my problem. I still do not live what happens in the CMD.
Added WaitForExit to the code in case thats the problem. Could you please explain your situation further?
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.