Semaphore.OpenExisting Method (String, SemaphoreRights)
Opens the specified named semaphore, if it already exists, with the desired security access.
Assembly: System (in System.dll)
[<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)>] static member OpenExisting : name:string * rights:SemaphoreRights -> Semaphore
- Type: System.String
The name of the system semaphore to open.
- Type: System.Security.AccessControl.SemaphoreRights
A bitwise combination of the enumeration values that represent the desired security access.
Return ValueType: System.Threading.Semaphore
An object that represents the named system semaphore.
name is an empty string.
name is longer than 260 characters.
name is a null reference (Nothing in Visual Basic).
The named semaphore does not exist.
A Win32 error occurred.
The named semaphore exists, but the user does not have the desired security access rights.
The OpenExisting method tries to open an existing named semaphore. If the system semaphore does not exist, this method throws an exception instead of creating the system semaphore. To create the system semaphore when it does not already exist, use one of the Semaphore constructors that has a name parameter.
Multiple calls to this method that use the same value for name do not necessarily return the same Semaphore object, even though the objects that are returned represent the same named system semaphore.
The following code example demonstrates the cross-process behavior of a named semaphore with access control security. The example uses the OpenExisting(String) method overload to test for the existence of a named semaphore.
If the semaphore does not exist, it is created with a maximum count of two and with access control security that denies the current user the right to use the semaphore, but grants the right to read and change permissions on the semaphore.
If you run the compiled example from two command windows, the second copy will throw an access violation exception on the call to the OpenExisting(String) method. The exception is caught, and the example uses the method overload to open the semaphore with the rights needed to read and change the permissions.
After the permissions are changed, the semaphore is opened with the rights required to enter and release it. If you run the compiled example from a third command window, it runs using the new permissions.