如何测试,如果另一个安装正在进行中? [英] How do I test if another installation is already in progress?
问题描述
假设我想自动在Windows的东西安装和我想尝试测试另一个安装是否正在尝试安装之前。我没有在安装程序的控制和必须这样做,在自动化框架。有没有更好的方式来做到这一点,一些Win32 API的?如果MSIEXEC运行不仅仅是测试?
[更新2]
改进我一直在使用,只是直接访问互斥前面的代码,这是很多更可靠的:
使用的System.Threading;
[...]
///<总结>
///等待(最多一个超时)为MSI安装服务成为自由的。
///< /总结>
///<退货和GT;
///一个成功的等待返回true,当安装程序服务已成为自由。
///等待安装服务时返回false已超过超时。
///< /回报>
公共静态布尔WaitForInstallerServiceToBeFree(时间跨度maxWaitTime)
{
//的_MSIExecute互斥体所使用的MSI安装服务,安装序列
//和防止多种基于MSI的安装在发生同一时间。
//欲了解更多信息:http://msdn.microsoft.com/en-us/library/aa372909(VS.85).aspx
常量字符串installerServiceMutexName =Global\\_MSIExecute ;
试
{
互斥MSIExecuteMutex = Mutex.OpenExisting(installerServiceMutexName,
System.Security.AccessControl.MutexRights.Synchronize);
布尔waitSuccess = MSIExecuteMutex.WaitOne(maxWaitTime,FALSE);
MSIExecuteMutex.ReleaseMutex();
返回waitSuccess;
}
赶上(WaitHandleCannotBeOpenedException)
{
//互斥锁不存在,则什么也不做
}
赶上(的ObjectDisposedException)
{
//互斥的处置打开它,并试图之间等待就可以了,什么都不做
}
返回真;
}
请参阅的说明< A HREF =http://msdn.microsoft.com/en-us/library/aa372909(VS.85).aspx> _MSIExecute互斥的MSDN上。
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 don't have control over the installer and have to do this in the automation framework. Is there a better way to do this, some win32 api?, than just testing if msiexec is running?
[Update 2]
Improved the previous code I had been using to just access the mutex directly, this is a lot more reliable:
using System.Threading;
[...]
/// <summary>
/// Wait (up to a timeout) for the MSI installer service to become free.
/// </summary>
/// <returns>
/// 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.
/// </returns>
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);
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;
}
See the description of the _MSIExecute Mutex on MSDN.
这篇关于如何测试,如果另一个安装正在进行中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!