8

In one of my integration tests, I have two threads that uninstall then install a program but when run in sequence they generate an error Failed to grab execution mutex. System error 258.

To get around this I have to sleep after the uninstall. I tried checking if the msiexec process was running but there were consistently 2-3 so it's not a good indicator. Is there a way to check if the msiexec execution mutex is available?

probably at the beach
  • 14,489
  • 16
  • 75
  • 116

1 Answers1

12
    bool msiIsRunning = false;
    try
    {
        using(var mutex = Mutex.OpenExisting(@"Global\_MSIExecute"))
        {
            msiIsRunning = true;
        }
    }
    catch (Exception)
    {
       // Mutex not found; MSI isn't running
    }
Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
  • BTW, remember that technically MSI isn't always a service. Win 9x platforms don't support windows services. – Christopher Painter Apr 13 '12 at 21:25
  • An old question, but it would be better to use Mutex.TryOpenExisting instead of Mutex.OpenExisting--exceptions shouldn't be used for program flow, especially in the very common case that MSI isn't running. – ReflexiveCode Apr 21 '20 at 21:27