How do I test if another installation is already in progress?

前端 未结 3 1096
时光说笑
时光说笑 2021-01-12 05:17

Assuming I\'m trying to automate the installation of something on windows and I want to try to test whether another installation is in progress before attempting install. I

3条回答
  •  长发绾君心
    2021-01-12 06:01

    I was getting an unhandled exception using the code above. I cross referenced this article witt this one

    Here's my updated code:

      /// 
    /// Wait (up to a timeout) for the MSI installer service to become free.
    /// 
    /// 
    /// Returns true for a successful wait, when the installer service has become free.
    /// Returns false when waiting for the installer service has exceeded the timeout.
    /// 
    public static bool IsMsiExecFree(TimeSpan maxWaitTime)
    {
        // The _MSIExecute mutex is used by the MSI installer service to serialize installations
        // and prevent multiple MSI based installations happening at the same time.
        // For more info: http://msdn.microsoft.com/en-us/library/aa372909(VS.85).aspx
        const string installerServiceMutexName = "Global\\_MSIExecute";
        Mutex MSIExecuteMutex = null;
        var isMsiExecFree = false;
        try
        {
                MSIExecuteMutex = Mutex.OpenExisting(installerServiceMutexName,
                                System.Security.AccessControl.MutexRights.Synchronize);
                isMsiExecFree = MSIExecuteMutex.WaitOne(maxWaitTime, false);
        }
            catch (WaitHandleCannotBeOpenedException)
            {
                // Mutex doesn't exist, do nothing
                isMsiExecFree = true;
            }
            catch (ObjectDisposedException)
            {
                // Mutex was disposed between opening it and attempting to wait on it, do nothing
                isMsiExecFree = true;
            }
            finally
            {
                if(MSIExecuteMutex != null && isMsiExecFree)
                MSIExecuteMutex.ReleaseMutex();
            }
        return isMsiExecFree;
    
    }
    

提交回复
热议问题