Reputation: 2911
So I launch a bunch of process to convert some audio files and i want my main program to wait until all of those process complete before executing.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo stratInfo = new System.Diagnostics.ProcessStartInfo();
stratInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
DirectoryInfo di = new DirectoryInfo(@dir);
foreach (FileInfo fi in di.GetFiles())
{
stratInfo.FileName = "C:\\AudioExtract.exe";
stratInfo.Arguments = "-a \"" + dir + "\\" + fi.Name + "\"";
process.StartInfo = stratInfo;
process.Start();
}
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("AudioExtract.exe"))
{
StatusLbl.Text = "Found!";
}
}
Thats what i have to see if it is running, but i need it to continue updating the getprocesses and check if it is still running and im not quite sure how.
My app launches Many of the same process for different audio files, almost simultaneously. I looked at the link in the comment and that will set up an event handler, how would i handle many of the same event everytime one of the processes exit?
Upvotes: 2
Views: 2768
Reputation: 19976
There are some issues that will make your code practically unusable:
EDIT:
How you could do it:
Upvotes: 3
Reputation: 524
inside the if, try
pprocess.WaitForExit();
break;
http://msdn.microsoft.com/en-us/library/fb4aw7b8.aspx
That code will wait until the process exits, then break from your foreach allowing your main program to continue running.
Upvotes: 3