当前位置:  开发笔记 > 编程语言 > 正文

如何测试其他安装是否已在进行中?

如何解决《如何测试其他安装是否已在进行中?》经验,为你挑选了1个好方法。

假设我正在尝试在Windows上自动安装某些东西,我想在尝试安装之前尝试测试是否正在进行其他安装.我无法控制安装程序,必须在自动化框架中执行此操作.有没有更好的方法来做这个,一些win32 api?,而不仅仅是测试msiexec是否正在运行?

[更新2]

改进了以前用于直接访问互斥锁的代码,这更加可靠:

using System.Threading;

[...]

/// 
/// 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 WaitForInstallerServiceToBeFree(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";

    try
    {
        Mutex MSIExecuteMutex = Mutex.OpenExisting(installerServiceMutexName, 
            System.Security.AccessControl.MutexRights.Synchronize | System.Security.AccessControl.MutexRights.Modify);
        bool waitSuccess = MSIExecuteMutex.WaitOne(maxWaitTime, false);
        MSIExecuteMutex.ReleaseMutex();
        return waitSuccess;
    }
    catch (WaitHandleCannotBeOpenedException)
    {
        // Mutex doesn't exist, do nothing
    }
    catch (ObjectDisposedException)
    {
        // Mutex was disposed between opening it and attempting to wait on it, do nothing
    }
    return true;
}

Mike Dimmick.. 6

请参阅MSDN上_MSIExecute Mutex的说明.



1> Mike Dimmick..:

请参阅MSDN上_MSIExecute Mutex的说明.

推荐阅读
小白也坚强_177
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有