Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Semaphore.OpenExisting Method (String, SemaphoreRights)

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

Parameters

name
Type: System.String

The name of the system semaphore to open.

rights
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.

ExceptionCondition
ArgumentException

name is an empty string.

-or-

name is longer than 260 characters.

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

WaitHandleCannotBeOpenedException

The named semaphore does not exist.

IOException

A Win32 error occurred.

UnauthorizedAccessException

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.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Show:
© 2015 Microsoft