Powershell 7 example
In a Powershell 7 session, we create a mutex:
PS C:\> [bool] $newMutex = $false
PS C:\> $mutex = new-object System.Threading.Mutex($true, 'mtxTQ84', [ref] $newMutex)
PS C:\> $newMutex
True
In another Powershell 7 session, we create a mutex with the same name.
After creating the mutex, the value of $newMutex
is False
and indicates that the name was already known:
PS C:\> [bool] $newMutex = $false
PS C:\> $mutex = new-object System.Threading.Mutex($true, 'mtxTQ84', [ref] $newMutex)
PS C:\> $newMutex
False
In the second session, we wait until the first session releases the mutex:
PS C:\> $mutex.WaitOne()
We then release the mutex in the first session. Note how the blocked second session becomes unblocked:
PS C:\> $mutex.ReleaseMutex()
We now wait in the first session until the second session releases the mutex ‥
PS C:\> $mutex.WaitOne()
‥ and release the mutex in the first session:
PS C:\> $mutex.ReleaseMutex()