No error is thrown. I would expect an error since a mutex by this name should already exist. Furthermore, the below does not return the mutex like I would expect (when run on the new console):
And finally, if I performed the below, I would expect a return code of “False” as the mutex should be busy. But I always get “True” (again, on the new console session):
We’ll see if anyone pops in with an answer, but you’re a bit outside the PowerShell-verse, here. StackOverflow might be a better spot to ask. Unfortunately, I know practically nothing about .NET’s implementation of mutexes to tell you if this is a problem related to PowerShell being essentially single-threaded, with its scope, the fact that PowerShell runs in the DLR, or something else entirely.
$CreatedMutex = ""
$Mutex = New-Object -TypeName system.threading.mutex($true, "Global\TestMutex", [ref] $CreatedMutex)
if (!$CreatedMutex) {
if (!$Mutex.WaitOne(5000)) {
throw "Timed out waiting for TestMutex"
}
}
# Do Stuff
$Mutex.ReleaseMutex()
$Mutex.Close()
Hope this helps…
SLDR
(Stephen L. De Rudder)
Note: It does look like that mutexes are always created as session level mutexes even when Global\TestMutex is used. So powershells that are on different sessions will not work. I don’t know if PowerShell run as admin will allow truly global mutexes to be created.