2008-09-26 16 views
7

Windows üzerinde bir şeyin kurulumunu otomatikleştirmeye çalışıyorum ve yükleme işlemini gerçekleştirmeden önce başka bir yüklemenin devam edip etmediğini test etmeye çalışmak istiyorum. Yükleyici üzerinde kontrol sahibi değilim ve bunu otomasyon çerçevesinde yapmak zorundayım. Bunu yapmak için daha iyi bir yol var mı, bazı win32 api ?, msiexec çalışıyorsa test etmekten daha mı?Başka bir yükleme işleminin devam edip etmediğini nasıl sınayabilirim?

[Güncelleme 2]

Sadece doğrudan Muteksleri erişmek için önceden kullandığım kod Geliştirilmiş, bu çok daha güvenilirdir:

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; 
} 
+2

MutexRights olmadan mevcut muteks'i açtığınızda. Bayrağı işaretlediğinizde, mutex'i serbest bırakamazsınız. Msdn: MutexRights.Synchronize, bir iş parçacığının muteks üzerinde beklemesine izin verir ve MutexRights.Modify, bir iş parçacığının ReleaseMutex yöntemini çağırmasına izin verir. http://msdn.microsoft.com/en-us/library/c41ybyt3.aspx –

cevap

2

Yukarıdaki kodu kullanarak işlenmeyen bir özel durum alıyorum. Bu makale Witt karşılaştırdım bu one

İşte benim güncellenen kod: Yayınladığınız kaçırma için

/// <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 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; 

} 
2

Üzgünüm!

Bu konu üzerinde çalışıyorum - yaklaşık bir hafta boyunca - notlarınızı (Teşekkürler) ve diğer sitelerin adlarını kullanarak çok fazla isim (hepinize teşekkür ederiz).

Hizmetin, MSIEXEC hizmetinin halihazırda kullanımda olup olmadığını belirlemek için yeterli bilgi verebileceğini belirten bilgiler karşısında tökezledim. Hizmet 'msiserver' - Windows Installer - ve bu bilgi hem durum hem de kabul edilir.

Aşağıdaki VBScript kodu bunu denetler.

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 
Check = False 
Do While Not Check 
    WScript.Sleep 3000 
    Set colServices = objWMIService.ExecQuery("Select * From Win32_Service Where Name="'msiserver'") 
    For Each objService In colServices 
     If (objService.Started And Not objService.AcceptStop) 
     WScript.Echo "Another .MSI is running." 
     ElseIf ((objService.Started And objService.AcceptStop) Or Not objService.Started) Then 
     WScript.Echo "Ready to install an .MSI application." 
     Check = True 
     End If 
    Next 
Loop 
İlgili konular