开发者

C# Detecting Spawned Processes

开发者 https://www.devze.com 2022-12-19 09:01 出处:网络
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 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开发者_如何学JAVA 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;


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消