-1

I am creating a GUI application using Visual C# 2010 (.NET Framework 3.5). How to change this my code to the no stops responding?

for (int i = listBox1.Items.Count - 1; i >= 0; i--)
{
    listBox1.SelectedIndex = i;
    Process myProcess = new Process();
    myProcess.StartInfo.FileName = "ffmpeg.exe";
    myProcess.StartInfo.WorkingDirectory = "";
    myProcess.StartInfo.Arguments = " -i \"" + listBox1.SelectedItem + "\" " + "-y ";
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    myProcess.Start();
    myProcess.WaitForExit();
    listBox1.Items.RemoveAt(i);
}

The convert program some time use the converting. How to show new form or messengebox the running application? I want it to respond the window. History for loop: How do I do I loop through items in a list box and then remove that item?

Community
  • 1
  • 1
Gabee8
  • 51
  • 1
  • 8
  • 2
    possible duplicate of [In C# How do you read from DOS program you executed if the program is constantly outputting?](http://stackoverflow.com/questions/22151235/in-c-sharp-how-do-you-read-from-dos-program-you-executed-if-the-program-is-const) – I4V Mar 06 '14 at 07:47

2 Answers2

7

WaitForExit, unsurprisingly, will block the thread until the process has exited.
Alternatively, you can use the Exited event to get notified when the process has exited.

You will also need to set the EnableRaisingEvents property to true in order for the Exited event to be fired.

myProcess.EnabledRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();

void myProcess_Exited(object sender, EventArgs e)
{
    //process has exited, implement logic here
    listBox1.Items.Remove...
}
Rotem
  • 21,452
  • 6
  • 62
  • 109
  • 2
    *WaitForExit, unsurprisingly, will block the thread until the process has exited* Mind = Blown – ta.speot.is Mar 06 '14 at 07:46
  • Start my process use selected listbox item and if exit process start new process and use next selection in listbox. while end listbox item(s). – Gabee8 Mar 06 '14 at 08:34
0

Thanks answer! Done this code:

        void myProcess_Exited(object sender, EventArgs e)
    {
        this.Invoke((MethodInvoker)delegate
        {

            if (listBox1.SelectedItem != null)
            {
                listBox1.Items.RemoveAt(0);
            }
            if (listBox1.Items.Count > 0)
            {
                button1.PerformClick();
            }
            else
            {
                MessageBox.Show("All Done!!");
            }
        });
    }
Gabee8
  • 51
  • 1
  • 8