Reputation: 11
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
}
}
Upvotes: 1
Views: 8872
Reputation: 168
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
}
}
Upvotes: 4
Reputation: 8162
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.
Upvotes: 0