1

I want to get the output of an execution in c# and I referred to this question. But I only have the output be printed on the console but not stored in the specified string. Here is my code: `

        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        //p.StartInfo.CreateNoWindow = true;

        p.StartInfo.FileName = "ffmpeg.exe";
        p.StartInfo.Arguments = " -i 1.flv";
        p.Start();


        p.WaitForExit();
        string output = p.StandardOutput.ReadToEnd();
        Console.WriteLine(output);
        Console.ReadLine();`

The output string is still empty after the execution of these codes. Plus if I keep the line p.StartInfo.CreateNoWindow = true;, no words will be printed on the console at all, why this happen? I thought the line will only stop a new window being created.

Community
  • 1
  • 1
GJ.
  • 101
  • 1
  • 2
  • 8
  • Have you tried reading in a loop, and monitoring the process state instead of using `WaitForExit`? I'd presume that after the process has exited, the standard output stream is closed. I had a similar problem: I started a console application from a windows service and wanted to send a command on the stdin to close the application when the service was stopped. What happened was that the service stopped unexpectedly and instead of getting an exception about the stream being closed unexpectedly, the console app kept reading empty lines in a 100% CPU loop. – Thorsten Dittmar Jul 08 '14 at 16:00

3 Answers3

1

Move string output = p.StandardOutput.ReadToEnd(); inside wait for exit. How are you going to read data when it already exit.

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    //p.StartInfo.CreateNoWindow = true;

    p.StartInfo.FileName = "ffmpeg.exe";
    p.StartInfo.Arguments = " -i 1.flv";
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

    Console.WriteLine(output);
    Console.ReadLine();`
Tsukasa
  • 6,342
  • 16
  • 64
  • 96
0

I'd try the following:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = " -i 1.flv";
p.Start();

while (!p.HasExited)
{
   string output = p.StandardOutput.ReadToEnd();
}

I also suggest you have a look at the BeginReadOutputLine method in this example given in the MS documentation. Being asynchronous, it will be called even though you use WaitForExit.

A boiled down version of this is:

// Start the asynchronous read of the output stream.
p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
p.EnableRaisingEvents = true;
p.BeginOutputReadLine();
p.Start();
p.WaitForExit();
p.Close();

private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
    // Collect the command output. 
    if (!String.IsNullOrEmpty(outLine.Data))
    {
        numOutputLines++;

        // Add the text to the output
        Console.WriteLine(Environment.NewLine + 
                "[" + numOutputLines.ToString() + "] - " + outLine.Data);
    }
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • thanks for help but I find the problem may probably come from the execution of ffmpeg itself so either synchronous or asynchronous method is feasible. I tried command like "ping" and "ipconfig" and they all worked well.@ThorstenDittmar – GJ. Jul 09 '14 at 05:55
0

How about switch those two line?

p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
yambe2002
  • 159
  • 4