C# 在for循环中处理WaitForExit时挂起
我正在使用Visual C#2010(.NET Framework 3.5)创建一个GUI应用程序。如何将此my代码更改为无停止响应C# 在for循环中处理WaitForExit时挂起,c#,C#,我正在使用Visual C#2010(.NET Framework 3.5)创建一个GUI应用程序。如何将此my代码更改为无停止响应 for (int i = listBox1.Items.Count - 1; i >= 0; i--) { listBox1.SelectedIndex = i; Process myProcess = new Process(); myProcess.StartInfo.FileName = "ffmpeg.exe"; my
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?我想让它响应窗口。
循环的历史记录:
WaitForExit
将阻塞线程,直到进程退出。或者,您可以使用事件在进程退出时获得通知 您还需要将
EnableRaisingEvents
属性设置为true
,以便触发退出的事件
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...
}
谢谢回答!
完成此代码:
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!!");
}
});
}
毫不奇怪,WaitForExit将阻止线程,直到进程退出Mind=BlownStart my process use selected listbox项,如果退出进程,则启动新进程并使用listbox中的下一个选择。而结束列表框项。可能重复