Semaphore.Release Method ()


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

Exits the semaphore and returns the previous count.

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

member Release : unit -> int

Return Value

Type: System.Int32

The count on the semaphore before the Release method was called.

Exception Condition

The semaphore count is already at the maximum value.


A Win32 error occurred with a named semaphore.


The current semaphore represents a named system semaphore, but the user does not have SemaphoreRights.Modify.


The current semaphore represents a named system semaphore, but it was not opened with SemaphoreRights.Modify.

Threads typically use the WaitOne method to enter the semaphore, and they typically use this method overload to exit.

If a SemaphoreFullException is thrown by the Release method, it does not necessarily indicate a problem with the calling thread. A programming error in another thread might have caused that thread to exit the semaphore more times than it entered.

If the current Semaphore object represents a named system semaphore, the user must have SemaphoreRights.Modify rights and the semaphore must have been opened with SemaphoreRights.Modify rights.

The following code example creates a semaphore with a maximum count of three and an initial count of zero. The example starts five threads, which block waiting for the semaphore. The main thread uses the Release(Int32) method overload to increase the semaphore count to its maximum, allowing three threads to enter the semaphore. Each thread uses the Thread.Sleep method to wait for one second, to simulate work, and then calls the Release() method overload to release the semaphore.

Each time the semaphore is released, the previous semaphore count is displayed. Console messages track semaphore use. The simulated work interval is increased slightly for each thread, to make the output easier to read.

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

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