Export (0) Print
Expand All

Semaphore.Release Method (Int32)

Exits the semaphore a specified number of times and returns the previous count.

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

member Release : 
        releaseCount:int -> int

Parameters

releaseCount
Type: System.Int32

The number of times to exit the semaphore.

Return Value

Type: System.Int32
The count on the semaphore before the Release method was called.

ExceptionCondition
ArgumentOutOfRangeException

releaseCount is less than 1.

SemaphoreFullException

The semaphore count is already at the maximum value.

IOException

A Win32 error occurred with a named semaphore.

UnauthorizedAccessException

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

-or-

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

If a thread has entered the semaphore multiple times, this method overload allows the entire semaphore count to be restored with one call.

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.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft