Semaphore Constructor (Int32, Int32, String)


Initializes a new instance of the Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries, and optionally specifying the name of a system semaphore object.

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

public Semaphore(
	int initialCount,
	int maximumCount,
	string name


Type: System.Int32

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

Type: System.Int32

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

Type: System.String

The name of a named system semaphore object.

Exception Condition

initialCount is greater than maximumCount.


name is longer than 260 characters.


maximumCount is less than 1.


initialCount is less than 0.


A Win32 error occurred.


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


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. If you need to determine whether or not a named system semaphore was created, use the Semaphore(Int32, Int32, String, Boolean) constructor overload instead.


When you use this constructor overload, the recommended practice is to specify the same number for initialCount and maximumCount. If initialCount is less than maximumCount, and a named system semaphore is created, the effect is the same as if the current thread had called WaitOne (maximumCount minus initialCount) times. However, with this constructor overload there is no way to determine whether a named system semaphore was created.

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.

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

If you want to find out whether a named system semaphore exists, use the OpenExisting method. The OpenExisting method attempts to open an existing named semaphore, and throws an exception if the system semaphore does not exist.

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

using System;
using System.Threading;

public class Example
    public static void Main()
        // Create a Semaphore object that represents the named 
        // system semaphore "SemaphoreExample3". The semaphore has a
        // maximum count of five. The initial count is also five. 
        // There is no point in using a smaller initial count,
        // because the initial count is not used if this program
        // doesn't create the named system semaphore, and with 
        // this method overload there is no way to tell. Thus, this
        // program assumes that it is competing with other
        // programs for the semaphore.
        Semaphore sem = new Semaphore(5, 5, "SemaphoreExample3");

        // Attempt to enter the semaphore three times. If another 
        // copy of this program is already running, only the first
        // two requests can be satisfied. The third blocks. Note 
        // that in a real application, timeouts should be used
        // on the WaitOne calls, to avoid deadlocks.
        Console.WriteLine("Entered the semaphore once.");
        Console.WriteLine("Entered the semaphore twice.");
        Console.WriteLine("Entered the semaphore three times.");

        // The thread executing this program has entered the 
        // semaphore three times. If a second copy of the program
        // is run, it will block until this program releases the 
        // semaphore at least once.
        Console.WriteLine("Enter the number of times to call Release.");
        int n;
        if (int.TryParse(Console.ReadLine(), out n))

        int remaining = 3 - n;
        if (remaining > 0)
            Console.WriteLine("Press Enter to release the remaining " +
                "count ({0}) and exit the program.", remaining);


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