Search code examples
c#.net-4.0waitforexit

Process.WaitForExit hangs while another installation is run


Good day! I'm working on installer, which installs additional dependencies for my software using Process.Start.

foreach dependency:
var process = System.Diagnostics.Process.Start(processStartInfo);
process.WaitForExit();

The problem is when another msi installation is runned, WaitForExit hangs (and when I close this another msi installation, WaitForExit also exits).

I can't use timeouts, because dependencies are different with very different installation time.

Is there any ways to handle this situation and correctly kill process (actually I want to know is dependency is installing or just hanging)? Many thanks.

Solution: in my case the problem is solved by checking if 'msiexec' process is running.


Solution

  • The solution to my problem - check global mutex, created by msiexec. This is also a correct way to check if another msi installation is running.

        public static bool WaitAnotherMsiInstallation(int timeout)
        {
            const string MsiMutexName = "Global\\_MSIExecute";
    
            try
            {
                using (var msiMutex = Mutex.OpenExisting(MsiMutexName, MutexRights.Synchronize))
                {
                    return msiMutex.WaitOne(timeout);
                }
            } 
            catch (WaitHandleCannotBeOpenedException)
            {
                // The named mutex does not exist.
                return true;
            }
            catch (ObjectDisposedException)
            {
                // Mutex was disposed between opening it and attempting to wait on it
                return true;
            }
        }
    

    Here is some details http://msdn.microsoft.com/en-us/library/aa372909(VS.85).aspx