Semaphore.TryOpenExisting Method (String, SemaphoreRights, Semaphore)

.NET Framework (current version)

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, and returns a value that indicates whether the operation succeeded.

Namespace:   System.Threading
Assembly:  System (in System.dll)

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static bool TryOpenExisting(
	string name,
	SemaphoreRights rights,
	out Semaphore result


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.

Type: System.Threading.Semaphore

When this method returns, contains a Semaphore object that represents the named semaphore if the call succeeded, or null if the call failed. This parameter is treated as uninitialized.

Return Value

Type: System.Boolean

true if the named semaphore was opened successfully; otherwise, false.

Exception Condition

name is an empty string.


name is longer than 260 characters.


name is null.


A Win32 error occurred.


The named semaphore exists, but the user does not have the security access required to use it.

If the named semaphore does not exist, this method does not create it. To create the system semaphore when it does not already exist, use one of the Semaphore constructors that has a name parameter.

If you are uncertain whether a named semaphore exists, use this method overload instead of the OpenExisting(String, SemaphoreRights) method overload, which throws an exception if the semaphore does not exist.

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.

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.

.NET Framework
Available since 4.5
Return to top