Semaphore.OpenExisting Method (String, SemaphoreRights)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Opens the specified named semaphore, if it already exists, with the desired security access.

Namespace:   System.Threading
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 Value

Type: System.Threading.Semaphore

An object that represents the named system semaphore.

Exception Condition

name is an empty string.


name is longer than 260 characters.


name is null.


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 rights parameter must include the SemaphoreRights.Synchronize flag to allow threads to enter the semaphore, and the SemaphoreRights.Modify flag to allow threads to call the Release method.

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 OpenExisting(String, SemaphoreRights) 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.

No code example is currently available or this language may not be supported.


for calling unmanaged code to create a named system semaphore. Associated enumeration: SecurityPermissionFlag.UnmanagedCode.

Security action: LinkDemand.

.NET Framework
Available since 2.0
Return to top