7

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;
2
  • Do you own the spawned processes? E.g. can you modify Installer2 if that would make it easier for you to detect completion? Or is Installer2 supplied by someone else, or you don't even know what additional processes might be spawned and need to handle arbitrary spawned processes?
    – itowlson
    Feb 9, 2010 at 2:16
  • We do own the particular installer in question. However we would also like to be able to handle arbitrary spawned processes.
    – tjjjohnson
    Feb 12, 2010 at 19:08

2 Answers 2

6

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=".

1
  • Be very VERY careful with this (I found out the hard way). Root level processes in Windows tend to be orphaned (wininit.exe, cssrss.exe) but still have a parent ID. The parent no longer exists, so its ID is used for new processes. When querying for a process' children, those orphaned root level processes are returned as well. Solution, check the start time of the children and make sure they were started AFTER the parent process. Jun 29, 2018 at 13:01
0

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.

1
  • 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
    Feb 9, 2010 at 2:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.