Ok, Basically, my C# Application is running an unattended installation of visual studio 2008 (its just a process, nothing complicated)

but i need a loop in there, so that when the setup.exe which is being called has finished running, i can call a message box to say its complete...

so my question is... in C# how do i setup a loop that checks when a process which is created in System.diagnostics.process is still running, so that when its not... i can display a message...

thanks in advance, I would be really greatful.

Not sure whether this is a good idea, but I believe it will do what you asked:

while (true)
            {
                // get all instances of notepad running...
                System.Diagnostics.Process[] procs = 
                    System.Diagnostics.Process.GetProcessesByName("notepad");

                if (procs.Count() == 0) // process by that name no longer running--we're done
                    break;

                System.Threading.Thread.Sleep(30000); // pause this app for 30 seconds
            }
            MessageBox.Show("Done");

There seems to be a Process.Exited event and also a Process.WaitForExit() function, maybe they could be of use?

There seems to be a Process.Exited event and also a Process.WaitForExit() function, maybe they could be of use?

Yes, you hit the nail on the head. Call Process.WaitForExit(); after you have started the installer. It will block the thread until the PID dies so you will probably want to run this inside of a backgroundWorker so your main thread can paint the UI until the installation is complete.

This article has been dead for over six months. Start a new discussion instead.