挂起进程WaitForExit for for循环

时间:2014-03-06 07:43:22

标签: c#

我正在使用Visual C#2010(.NET Framework 3.5)创建GUI应用程序。如何将我的代码更改为不停止响应?

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);
}

转换程序有时会使用转换。如何在正在运行的应用程序中显示新表单或messengebox?我希望它能回应窗口。 循环历史: How do I do I loop through items in a list box and then remove that item?

2 个答案:

答案 0 :(得分:7)

毫不奇怪,

WaitForExit将阻止该线程,直到该进程退出 或者,您可以使用Exited事件在流程退出时收到通知。

您还需要将EnableRaisingEvents属性设置为true,以便触发Exited事件。

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...
}

答案 1 :(得分:0)

谢谢回答! 完成此代码:

        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!!");
            }
        });
    }