0
votes

The following code shows error as "StandardOut has not been redirected or the process hasn't started yet." What is the problem in this code? It requires any changes? It always clear the process by catch exception.

static void ExecuteAsync()
            {
                if (File.Exists("Videos/output.flv"))
                try
                {
                    File.Delete("Videos/output.flv");
                }
                catch
                {
                    return;
                }

            try
            {
                process = new Process();
                ProcessStartInfo info = new ProcessStartInfo(@"e:\ffmpeg\bin\ffmpeg.exe", "-i cars1.flv -same_quant intermediate1.mpg");
                info.CreateNoWindow = false;
                info.UseShellExecute = false;
                info.RedirectStandardError = true;
                info.RedirectStandardOutput = true;
                process.StartInfo = info;
                process.EnableRaisingEvents = true;
                process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
                process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
                process.Exited += new EventHandler(process_Exited);
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
            }
            catch (Exception ex)
            {
                if (process != null) process.Dispose();
            }
        }
        static int lineCount = 0;
        static void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("Input line: {0} ({1:m:s:fff})", lineCount++, DateTime.Now);
            Console.WriteLine(e.Data);
            Console.WriteLine();
        }

        static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("Output Data Received.");
        }

        static void process_Exited(object sender, EventArgs e)
        {
            process.Dispose();
            Console.WriteLine("Bye bye!");
        }
    }
1

1 Answers

2
votes

set this to false:

info.RedirectStandardOutput = false;

Documentation says :

To use StandardOutput, you must set ProcessStartInfo..::.UseShellExecute to false, and you must set ProcessStartInfo..::.RedirectStandardOutput to true. Otherwise, reading from the StandardOutput stream throws an exception