Semaphore.OpenExisting Method (String)

 
System_CAPS_noteNote

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

Opens the specified named semaphore, if it already exists.

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

[<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)>]
static member OpenExisting : 
        name:string -> Semaphore

Parameters

name
Type: System.String

The name of the system semaphore to open.

Return Value

Type: System.Threading.Semaphore

An object that represents the named system semaphore.

Exception Condition
ArgumentException

name is an empty string.

-or-

name is longer than 260 characters.

ArgumentNullException

name is null.

WaitHandleCannotBeOpenedException

The named semaphore does not exist.

IOException

A Win32 error occurred.

UnauthorizedAccessException

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

The OpenExisting method tries to open the specified 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.

This method overload is equivalent to calling the OpenExisting method overload and specifying SemaphoreRights.Synchronize and SemaphoreRights.Modify rights, combined by using the bitwise OR operation.

Specifying the SemaphoreRights.Synchronize flag allows a thread to enter the semaphore, and specifying the SemaphoreRights.Modify flag allows a thread to call the Release method.

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 which 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 overload. 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.

SecurityPermission

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

Security action: LinkDemand.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: