Represents a lightweight alternative to Semaphore that limits the number of threads that can access a resource or pool of resources concurrently.
Assembly: mscorlib (in mscorlib.dll)
Initializes a new instance of theclass, specifying the initial number of requests that can be granted concurrently.
Initializes a new instance of theclass, specifying the initial and maximum number of requests that can be granted concurrently.
Releases all resources used by the current instance of theclass.
Releases the unmanaged resources used by the, and optionally releases the managed resources.
Determines whether the specified object is equal to the current object.(Inherited from Object.)
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)
Serves as the default hash function. (Inherited from Object.)
Releases theobject once.
Releases theobject a specified number of times.
Returns a string that represents the current object.(Inherited from Object.)
Blocks the current thread until it can enter the.
Blocks the current thread until it can enter the CancellationToken., while observing a
Blocks the current thread until it can enter the, using a 32-bit signed integer that specifies the timeout.
Blocks the current thread until it can enter the CancellationToken., using a 32-bit signed integer that specifies the timeout, while observing a
Blocks the current thread until it can enter the TimeSpan to specify the timeout., using a
Asynchronously waits to enter the.
Asynchronously waits to enter the CancellationToken., while observing a
Asynchronously waits to enter the, using a 32-bit signed integer to measure the time interval.
Asynchronously waits to enter the CancellationToken., using a 32-bit signed integer to measure the time interval, while observing a
Asynchronously waits to enter the TimeSpan to measure the time interval., using a
Semaphores are of two types: local semaphores and named system semaphores. The former is local to an app. The latter is visible throughout the operating system and is suitable for inter-process synchronization. The Semaphore class that doesn't use Windows kernel semaphores. Unlike the Semaphore class, the class doesn’t support named system semaphores. You can use it as a local semaphore only. The class is the recommended semaphore for synchronization within a single app.is a lightweight alternative to the
A lightweight semaphore controls access to a pool of resources that is local to your application. When you instantiate a semaphore, you can specify the maximum number of threads that can enter the semaphore concurrently. You also specify the initial number of threads that can enter the semaphore concurrently. This defines the semaphore's count.
The count is decremented each time a thread enters the semaphore, and incremented each time a thread releases the semaphore. To enter the semaphore, a thread calls one of the Wait or WaitAsync overloads. To release the semaphore, it calls one of the Release overloads. When the count reaches zero, subsequent calls to one of the Wait methods block until other threads release the semaphore. If multiple threads are blocked, there is no guaranteed order, such as FIFO or LIFO, that controls when threads enter the semaphore.
The basic structure for code that uses a semaphore to protect resources is:
' Enter semaphore by calling one of the Wait or WaitAsync methods. SemaphoreSlim.Wait() ' ' Execute code protected by the semaphore. ' SemaphoreSlim.Release()
When all threads have released the semaphore, the count is at the maximum value specified when the semaphore was created. The semaphore's count is available from the CurrentCount property.
The Wait, WaitAsync, and Release methods. In addition, if the SemaphoreSlim(Int32) constructor is used to instantiate the object, the CurrentCount property can increase beyond the value set by the constructor. It is the programmer's responsibility to ensure that calls to Wait or WaitAsync methods are appropriately paired with calls to Release methods.class doesn’t enforce thread or task identity on calls to the
The following example creates a semaphore with a maximum count of three threads and an initial count of zero threads. The example starts five tasks, all of which block waiting for the semaphore. The main thread calls the Release(Int32) overload to increase the semaphore count to its maximum, which allows three tasks to enter 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.
Available since 4.5
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Available since 8.1
All public and protected members of Dispose, which must be used only when all other operations on the have completed.are thread-safe and may be used concurrently from multiple threads, with the exception of