Как запустить Process() один за другим в цикле?C#

Место общения программистов C#
Anonymous
Как запустить Process() один за другим в цикле?

Сообщение Anonymous »

Я передаю путь к файлу процессу в цикле for и позволяю ffmpeg.exe каждый раз читать только один формат видеофайла. Однако он останавливается после того, как текст текстового поля содержит содержимое первого видео.
Я пробовал процессы.close() иprocess.hasExited, но они не смогли помешать программе запустить более одного процесса, что привело к тому, что делегат заполнил текстовое поле (выходные данные были перечислены в неправильном порядке, поэтому я не мог определить, какие строки принадлежат какому видео, и некоторые выходные данные отсутствовали или отображались дважды).
Как это сделать? Я выполняю только один процесс за раз, прежде чем запустится следующий, в цикле for?
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++;
}

Вернуться в «C#»