Semaphore Constructor (Int32, Int32, String, Boolean)

 

Initializes a new instance of the Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries, optionally specifying the name of a system semaphore object, and specifying a variable that receives a value indicating whether a new system semaphore was created.

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

new : 
        initialCount:int *
        maximumCount:int *
        name:string *
        createdNew:bool byref -> Semaphore

Parameters

initialCount
Type: System.Int32

The initial number of requests for the semaphore that can be satisfied concurrently.

maximumCount
Type: System.Int32

The maximum number of requests for the semaphore that can be satisfied concurrently.

name
Type: System.String

The name of a named system semaphore object.

createdNew
Type: System.Boolean

When this method returns, contains true if a local semaphore was created (that is, if name is null or an empty string) or if the specified named system semaphore was created; false if the specified named system semaphore already existed. This parameter is passed uninitialized.

Exception Condition
ArgumentException

initialCount is greater than maximumCount.

-or-

name is longer than 260 characters.

ArgumentOutOfRangeException

maximumCount is less than 1.

-or-

initialCount is less than 0.

IOException

A Win32 error occurred.

UnauthorizedAccessException

The named semaphore exists and has access control security, and the user does not have SemaphoreRights.FullControl.

WaitHandleCannotBeOpenedException

The named semaphore cannot be created, perhaps because a wait handle of a different type has the same name.

This constructor initializes a Semaphore object that represents a named system semaphore. You can create multiple Semaphore objects that represent the same named system semaphore.

If the named system semaphore does not exist, it is created with the initial count and maximum count specified by initialCount and maximumCount. If the named system semaphore already exists, initialCount and maximumCount are not used, although invalid values still cause exceptions. Use createdNew to determine whether the system semaphore was created.

If initialCount is less than maximumCount, and createdNew is true, the effect is the same as if the current thread had called WaitOne (maximumCount minus initialCount) times.

If you specify null or an empty string for name, a local semaphore is created, as if you had called the Semaphore(Int32, Int32) constructor overload. In this case, createdNew is always true.

Because named semaphores are visible throughout the operating system, they can be used to coordinate resource use across process boundaries.

The following code example demonstrates the cross-process behavior of a named semaphore. The example creates a named semaphore with a maximum count of five and an initial count of two. That is, it reserves three entries for the thread that calls the constructor. If createNew is false, the program makes three calls to the WaitOne method. Thus, if you run the compiled example from two command windows, the second copy will block on the third call to WaitOne. Release one or more entries in the first copy of the program to unblock the second.

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

SecurityPermission

for calling unmanaged code to create 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: