Domanda

I use the following code to check if a Single Instance of the CustomBA is already running in the Run block of the CustomBA.

When the user starts the "setup.exe" (CustomBA) by double clicking it the code below returns true which is the expected behaviour.

However when the user right clicks and starts it as administrator the code returns false. Why is that?

private bool IsSingleInstanceOfSetupRunning()
{
    bool result = true;

    Process currentProcess = Process.GetCurrentProcess();
    if (Process.GetProcessesByName(currentProcess.ProcessName).Length > 1)
    {
        result = false;
    }

    return result;
}
È stato utile?

Soluzione

It appears that the WiX Engine detects that the process is running as admin and spins up the secondary process used for actually installing the MSIs. So there really are two processes running with the same name.

You can see the same behavior with the non-admin process once your CustomBA code calls Engine.Apply(). This is typically when the user sees a UAC prompt as the Engine spins up the second, elevated process to handle the actual MSI installs.

Since the main process is already running as admin, and no UAC prompting will occur by spinning up the second process, the Engine goes ahead and starts it immediately instead of waiting for the call to Engine.Apply().

Also note: if you're performing a Major Upgrade, the uninstall of the prior version will be run (in silent mode) during the upgrade, which would result in additional processes. You need to make sure you allow the uninstall process to run even if there is another process already running (your upgrade process).

One approach would be to use a mutex to do the checking, but only when running in DisplayMode Display.Full:

if (DisplayMode == Display.Full)
{
    bool mutexCreated = false;
    mutex = new Mutex(true, @"My Installer F1096BB9-CFDF-4AD1-91D8-9AA8805784A8", out mutexCreated);
    if (!mutexCreated)
    {
        MessageBox.Show("Another instance of the installer is already running. You may only run one at a time.",
                        "Installer already running", MessageBoxButton.OK,
                        MessageBoxImage.Warning);
        Log("Installer already running");
        Exit(ActionResult.NotExecuted);
    }
}

public void Exit(ActionResult actionResult)
{

    if (mutex != null)
    {
        mutex.Close();
        mutex = null;
    }

    Log(string.Format("Exiting with code {0}", actionResult));
    Engine.Quit((int) actionResult);    
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top