I am trying to pass a file path to the Process in a for loop and let ffmpeg.exe read only one video file's format each time. However, it stops after the textbox's text contains the first video's content. I have tried process.close(), process.hasExited, etc, but those couldn't stop the program from starting more than one Process and caused the delegate massively updated the textbox (the outputs weren't listed in the right order and I couldn't tell which lines belong to which video, and a few outputs were missing or showing twice in the text). How to execute only one process each time before the next in a for loop?
foreach (String file in inputList)
{
if (flag == true)
{
flag = false;
//textBox1.Text += file + Environment.NewLine;
checkvideoFormat(file);
}
}
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void checkvideoFormat(String filePath)
{
Process process1 = new Process();
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.FileName = ".\\ffmpeg.exe";
process1.StartInfo.WorkingDirectory = ".\\";
process1.EnableRaisingEvents = true;
process1.StartInfo.RedirectStandardOutput = true; //if this is true, UseShellExecute must be false. true if output should be written to StandardOutput
process1.StartInfo.RedirectStandardError = true;
//indicates that the associated Process has written a line that's terminated with a newline
process1.ErrorDataReceived += new DataReceivedEventHandler(inputHandler);
process1.Exited += (ending, p) =>
{
flag = true;
Console.WriteLine(flag);
//textBox1.Text += "Hey " + file + Environment.NewLine;
//process1.CancelOutputRead();
//process1.CancelErrorRead();//
};
process1.StartInfo.Arguments = "-i " + " \"" + filePath + "\"";
Console.WriteLine(process1.StartInfo.Arguments);
process1.Start();
process1.BeginOutputReadLine();
process1.BeginErrorReadLine();
//process1.Close();
//process1.WaitForExit();
/*
if(process1.HasExited == true)
{
//process1.Refresh();
flag = true;
//process1.Close();
//process1.Kill();
Thread.Sleep(1000);
}
*/
}
int test_123 = 0;
private void inputHandler(object sender, DataReceivedEventArgs l)
{
cba.Append(test_123 + l.Data + "\n");
videoInput = test_123 + l.Data + Environment.NewLine;
//Console.WriteLine(cba);
//Process p = sender as Process;
Console.WriteLine(videoInput);
this.Invoke(new MethodInvoker(() =>
{
if (!String.IsNullOrEmpty(videoInput))
{
textBox1.Text += videoInput;
}
}));
test_123++;
}