views:

235

answers:

2

I'm writing a piece of c# code that launches an installer and waits for it to return before continuing with other stuff.

I'm having trouble with certain installers that spawn other processes with the original process returning before the install has actual finished. Is there some way that I can wait until all the processes have finished?

To clarify here's the scenario I'm having trouble with:

  1. Launch Installer1
  2. Installer1 spawns/launches another installer (Installer2)
  3. Installer 1 returns
  4. Application thinks install has finished but Installer2 is still running. This causes issues with workflow in the app.

Here's the code I'm using at the moment:

// launch installer
Process process = windowsApplicationLauncher.LaunchApplication(_localFilePath);

// wait for process to return
do
{
    if (!process.HasExited)
    {
    }
}
while (!process.WaitForExit(1000));

if (process.ExitCode == 0)
{
    _fileService.MoveFile(_localFilePath, _postInstallFilePath);

    _notification.SetComplete(false);

    return true;
}
return false;
A: 

It might be better to do it this way inside the do / while loop:

System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(proc.ProcessName, Environment.MachineName);

Then iterate through the procs to find out which is still running...by using the HasExited property...

The logic being that the process's subprocesses are owned by your code, so you could check first if they have exited or not, if not, keep looping...

Hope this helps, Best regards, Tom.

tommieb75
That gets me a list of processes with the same name as the one I've launched (setup.exe) I'm interested in the msiexec.exe processes that are spawned by that setup.
tjjjohnson
+4  A: 

Have you thought about using WMI to solve this problem?

You can use WMI to listen for process creation and deletion events. Question 967668 has a good example.

When you receive a process creation event, you could issue a WMI query to determine if the process is a child (or a child of a child etc) of your root installer with something like the following:

"SELECT * FROM Win32_Process WHERE ParentProcessId=".

Antony Perkov
thanks for that very helpful.
tjjjohnson