mscorlib(4.0.0.0) API with additions
System.Threading.Semaphore Class Reference

Limits the number of threads that can access a resource or pool of resources concurrently. More...

Inheritance diagram for System.Threading.Semaphore:
[legend]
Collaboration diagram for System.Threading.Semaphore:
[legend]

Public Member Functions

 Semaphore (int initialCount, int maximumCount)
 Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries. More...
 
 Semaphore (int initialCount, int maximumCount, string name)
 Initializes a new instance of the T:System.Threading.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. More...
 
 Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
 Initializes a new instance of the T:System.Threading.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. More...
 
unsafe Semaphore (int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity)
 Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries, optionally specifying the name of a system semaphore object, specifying a variable that receives a value indicating whether a new system semaphore was created, and specifying security access control for the system semaphore. More...
 
int Release ()
 Exits the semaphore and returns the previous count. More...
 
int Release (int releaseCount)
 Exits the semaphore a specified number of times and returns the previous count. More...
 
SemaphoreSecurity GetAccessControl ()
 Gets the access control security for a named system semaphore. More...
 
void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
 Sets the access control security for a named system semaphore. More...
 
- Public Member Functions inherited from System.Threading.WaitHandle
virtual bool WaitOne (int millisecondsTimeout, bool exitContext)
 Blocks the current thread until the current T:System.Threading.WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval and specifying whether to exit the synchronization domain before the wait. More...
 
virtual bool WaitOne (TimeSpan timeout, bool exitContext)
 Blocks the current thread until the current instance receives a signal, using a T:System.TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait. More...
 
virtual bool WaitOne ()
 Blocks the current thread until the current T:System.Threading.WaitHandle receives a signal. More...
 
virtual bool WaitOne (int millisecondsTimeout)
 Blocks the current thread until the current T:System.Threading.WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval in milliseconds. More...
 
virtual bool WaitOne (TimeSpan timeout)
 Blocks the current thread until the current instance receives a signal, using a T:System.TimeSpan to specify the time interval. More...
 
virtual void Close ()
 Releases all resources held by the current T:System.Threading.WaitHandle. More...
 
void Dispose ()
 Releases all resources used by the current instance of the T:System.Threading.WaitHandle class. More...
 
- Public Member Functions inherited from System.MarshalByRefObject
object GetLifetimeService ()
 Retrieves the current lifetime service object that controls the lifetime policy for this instance. More...
 
virtual object InitializeLifetimeService ()
 Obtains a lifetime service object to control the lifetime policy for this instance. More...
 
virtual ObjRef CreateObjRef (Type requestedType)
 Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. More...
 

Static Public Member Functions

static Semaphore OpenExisting (string name)
 Opens the specified named semaphore, if it already exists. More...
 
static Semaphore OpenExisting (string name, SemaphoreRights rights)
 Opens the specified named semaphore, if it already exists, with the desired security access. More...
 
static bool TryOpenExisting (string name, out Semaphore result)
 Opens the specified named semaphore, if it already exists, and returns a value that indicates whether the operation succeeded. More...
 
static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
 Opens the specified named semaphore, if it already exists, with the desired security access, and returns a value that indicates whether the operation succeeded. More...
 
- Static Public Member Functions inherited from System.Threading.WaitHandle
static bool WaitAll (WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
 Waits for all the elements in the specified array to receive a signal, using an T:System.Int32 value to specify the time interval and specifying whether to exit the synchronization domain before the wait. More...
 
static bool WaitAll (WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext)
 Waits for all the elements in the specified array to receive a signal, using a T:System.TimeSpan value to specify the time interval, and specifying whether to exit the synchronization domain before the wait. More...
 
static bool WaitAll (WaitHandle[] waitHandles)
 Waits for all the elements in the specified array to receive a signal. More...
 
static bool WaitAll (WaitHandle[] waitHandles, int millisecondsTimeout)
 Waits for all the elements in the specified array to receive a signal, using an T:System.Int32 value to specify the time interval. More...
 
static bool WaitAll (WaitHandle[] waitHandles, TimeSpan timeout)
 Waits for all the elements in the specified array to receive a signal, using a T:System.TimeSpan value to specify the time interval. More...
 
static int WaitAny (WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
 Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed integer to specify the time interval, and specifying whether to exit the synchronization domain before the wait. More...
 
static int WaitAny (WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext)
 Waits for any of the elements in the specified array to receive a signal, using a T:System.TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait. More...
 
static int WaitAny (WaitHandle[] waitHandles, TimeSpan timeout)
 Waits for any of the elements in the specified array to receive a signal, using a T:System.TimeSpan to specify the time interval. More...
 
static int WaitAny (WaitHandle[] waitHandles)
 Waits for any of the elements in the specified array to receive a signal. More...
 
static int WaitAny (WaitHandle[] waitHandles, int millisecondsTimeout)
 Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed integer to specify the time interval. More...
 
static bool SignalAndWait (WaitHandle toSignal, WaitHandle toWaitOn)
 Signals one T:System.Threading.WaitHandle and waits on another. More...
 
static bool SignalAndWait (WaitHandle toSignal, WaitHandle toWaitOn, TimeSpan timeout, bool exitContext)
 Signals one T:System.Threading.WaitHandle and waits on another, specifying the time-out interval as a T:System.TimeSpan and specifying whether to exit the synchronization domain for the context before entering the wait. More...
 
static bool SignalAndWait (WaitHandle toSignal, WaitHandle toWaitOn, int millisecondsTimeout, bool exitContext)
 Signals one T:System.Threading.WaitHandle and waits on another, specifying a time-out interval as a 32-bit signed integer and specifying whether to exit the synchronization domain for the context before entering the wait. More...
 

Additional Inherited Members

- Public Attributes inherited from System.Threading.WaitHandle
const int WaitTimeout = 258
 Indicates that a M:System.Threading.WaitHandle.WaitAny(System.Threading.WaitHandle[],System.Int32,System.Boolean) operation timed out before any of the wait handles were signaled. This field is constant. More...
 
- Protected Member Functions inherited from System.Threading.WaitHandle
 WaitHandle ()
 Initializes a new instance of the T:System.Threading.WaitHandle class. More...
 
virtual void Dispose (bool explicitDisposing)
 When overridden in a derived class, releases the unmanaged resources used by the T:System.Threading.WaitHandle, and optionally releases the managed resources. More...
 
- Protected Member Functions inherited from System.MarshalByRefObject
MarshalByRefObject MemberwiseClone (bool cloneIdentity)
 Creates a shallow copy of the current T:System.MarshalByRefObject object. More...
 
- Static Protected Attributes inherited from System.Threading.WaitHandle
static readonly IntPtr InvalidHandle = GetInvalidHandle()
 Represents an invalid native operating system handle. This field is read-only. More...
 
- Properties inherited from System.Threading.WaitHandle
virtual IntPtr Handle [get, set]
 Gets or sets the native operating system handle. More...
 
SafeWaitHandle SafeWaitHandle [get, set]
 Gets or sets the native operating system handle. More...
 

Detailed Description

Limits the number of threads that can access a resource or pool of resources concurrently.

Definition at line 16 of file Semaphore.cs.

Constructor & Destructor Documentation

◆ Semaphore() [1/4]

System.Threading.Semaphore.Semaphore ( int  initialCount,
int  maximumCount 
)

Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries.

Parameters
initialCountThe initial number of requests for the semaphore that can be granted concurrently.
maximumCountThe maximum number of requests for the semaphore that can be granted concurrently.
Exceptions
T:System.ArgumentExceptioninitialCount is greater than maximumCount .
T:System.ArgumentOutOfRangeExceptionmaximumCount is less than 1.-or- initialCount is less than 0.

Definition at line 38 of file Semaphore.cs.

◆ Semaphore() [2/4]

System.Threading.Semaphore.Semaphore ( int  initialCount,
int  maximumCount,
string  name 
)

Initializes a new instance of the T:System.Threading.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.

Parameters
initialCountThe initial number of requests for the semaphore that can be granted concurrently.
maximumCountThe maximum number of requests for the semaphore that can be granted concurrently.
nameThe name of a named system semaphore object.
Exceptions
T:System.ArgumentExceptioninitialCount is greater than maximumCount .-or- name is longer than 260 characters.
T:System.ArgumentOutOfRangeExceptionmaximumCount is less than 1.-or- initialCount is less than 0.
T:System.IO.IOExceptionA Win32 error occurred.
T:System.UnauthorizedAccessExceptionThe named semaphore exists and has access control security, and the user does not have F:System.Security.AccessControl.SemaphoreRights.FullControl.
T:System.Threading.WaitHandleCannotBeOpenedExceptionThe named semaphore cannot be created, perhaps because a wait handle of a different type has the same name.

Definition at line 58 of file Semaphore.cs.

◆ Semaphore() [3/4]

System.Threading.Semaphore.Semaphore ( int  initialCount,
int  maximumCount,
string  name,
out bool  createdNew 
)

Initializes a new instance of the T:System.Threading.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.

Parameters
initialCountThe initial number of requests for the semaphore that can be satisfied concurrently.
maximumCountThe maximum number of requests for the semaphore that can be satisfied concurrently.
nameThe name of a named system semaphore object.
createdNewWhen 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.
Exceptions
T:System.ArgumentExceptioninitialCount is greater than maximumCount . -or- name is longer than 260 characters.
T:System.ArgumentOutOfRangeExceptionmaximumCount is less than 1.-or- initialCount is less than 0.
T:System.IO.IOExceptionA Win32 error occurred.
T:System.UnauthorizedAccessExceptionThe named semaphore exists and has access control security, and the user does not have F:System.Security.AccessControl.SemaphoreRights.FullControl.
T:System.Threading.WaitHandleCannotBeOpenedExceptionThe named semaphore cannot be created, perhaps because a wait handle of a different type has the same name.

Definition at line 105 of file Semaphore.cs.

◆ Semaphore() [4/4]

unsafe System.Threading.Semaphore.Semaphore ( int  initialCount,
int  maximumCount,
string  name,
out bool  createdNew,
SemaphoreSecurity  semaphoreSecurity 
)

Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries, optionally specifying the name of a system semaphore object, specifying a variable that receives a value indicating whether a new system semaphore was created, and specifying security access control for the system semaphore.

Parameters
initialCountThe initial number of requests for the semaphore that can be satisfied concurrently.
maximumCountThe maximum number of requests for the semaphore that can be satisfied concurrently.
nameThe name of a named system semaphore object.
createdNewWhen 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.
semaphoreSecurityA T:System.Security.AccessControl.SemaphoreSecurity object that represents the access control security to be applied to the named system semaphore.
Exceptions
T:System.ArgumentExceptioninitialCount is greater than maximumCount .-or- name is longer than 260 characters.
T:System.ArgumentOutOfRangeExceptionmaximumCount is less than 1.-or- initialCount is less than 0.
T:System.UnauthorizedAccessExceptionThe named semaphore exists and has access control security, and the user does not have F:System.Security.AccessControl.SemaphoreRights.FullControl.
T:System.IO.IOExceptionA Win32 error occurred.
T:System.Threading.WaitHandleCannotBeOpenedExceptionThe named semaphore cannot be created, perhaps because a wait handle of a different type has the same name.

Definition at line 126 of file Semaphore.cs.

Member Function Documentation

◆ GetAccessControl()

SemaphoreSecurity System.Threading.Semaphore.GetAccessControl ( )

Gets the access control security for a named system semaphore.

Returns
A T:System.Security.AccessControl.SemaphoreSecurity object that represents the access control security for the named system semaphore.
Exceptions
T:System.UnauthorizedAccessExceptionThe current T:System.Threading.Semaphore object represents a named system semaphore, and the user does not have F:System.Security.AccessControl.SemaphoreRights.ReadPermissions rights.-or-The current T:System.Threading.Semaphore object represents a named system semaphore and was not opened with F:System.Security.AccessControl.SemaphoreRights.ReadPermissions rights.
T:System.NotSupportedExceptionNot supported for Windows 98 or Windows Millennium Edition.

Definition at line 344 of file Semaphore.cs.

◆ OpenExisting() [1/2]

static Semaphore System.Threading.Semaphore.OpenExisting ( string  name)
static

Opens the specified named semaphore, if it already exists.

Parameters
nameThe name of the system semaphore to open.
Returns
An object that represents the named system semaphore.
Exceptions
T:System.ArgumentExceptionname is an empty string.-or- name is longer than 260 characters.
T:System.ArgumentNullExceptionname is null.
T:System.Threading.WaitHandleCannotBeOpenedExceptionThe named semaphore does not exist.
T:System.IO.IOExceptionA Win32 error occurred.
T:System.UnauthorizedAccessExceptionThe named semaphore exists, but the user does not have the security access required to use it.

Definition at line 193 of file Semaphore.cs.

◆ OpenExisting() [2/2]

static Semaphore System.Threading.Semaphore.OpenExisting ( string  name,
SemaphoreRights  rights 
)
static

Opens the specified named semaphore, if it already exists, with the desired security access.

Parameters
nameThe name of the system semaphore to open.
rightsA bitwise combination of the enumeration values that represent the desired security access.
Returns
An object that represents the named system semaphore.
Exceptions
T:System.ArgumentExceptionname is an empty string.-or- name is longer than 260 characters.
T:System.ArgumentNullExceptionname is null.
T:System.Threading.WaitHandleCannotBeOpenedExceptionThe named semaphore does not exist.
T:System.IO.IOExceptionA Win32 error occurred.
T:System.UnauthorizedAccessExceptionThe named semaphore exists, but the user does not have the desired security access rights.

Definition at line 211 of file Semaphore.cs.

◆ Release() [1/2]

int System.Threading.Semaphore.Release ( )

Exits the semaphore and returns the previous count.

Returns
The count on the semaphore before the Overload:System.Threading.Semaphore.Release method was called.
Exceptions
T:System.Threading.SemaphoreFullExceptionThe semaphore count is already at the maximum value.
T:System.IO.IOExceptionA Win32 error occurred with a named semaphore.
T:System.UnauthorizedAccessExceptionThe current semaphore represents a named system semaphore, but the user does not have F:System.Security.AccessControl.SemaphoreRights.Modify.-or-The current semaphore represents a named system semaphore, but it was not opened with F:System.Security.AccessControl.SemaphoreRights.Modify.

Definition at line 312 of file Semaphore.cs.

◆ Release() [2/2]

int System.Threading.Semaphore.Release ( int  releaseCount)

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

Parameters
releaseCountThe number of times to exit the semaphore.
Returns
The count on the semaphore before the Overload:System.Threading.Semaphore.Release method was called.
Exceptions
T:System.ArgumentOutOfRangeExceptionreleaseCount is less than 1.
T:System.Threading.SemaphoreFullExceptionThe semaphore count is already at the maximum value.
T:System.IO.IOExceptionA Win32 error occurred with a named semaphore.
T:System.UnauthorizedAccessExceptionThe current semaphore represents a named system semaphore, but the user does not have F:System.Security.AccessControl.SemaphoreRights.Modify rights.-or-The current semaphore represents a named system semaphore, but it was not opened with F:System.Security.AccessControl.SemaphoreRights.Modify rights.

Definition at line 327 of file Semaphore.cs.

◆ SetAccessControl()

void System.Threading.Semaphore.SetAccessControl ( SemaphoreSecurity  semaphoreSecurity)

Sets the access control security for a named system semaphore.

Parameters
semaphoreSecurityA T:System.Security.AccessControl.SemaphoreSecurity object that represents the access control security to be applied to the named system semaphore.
Exceptions
T:System.ArgumentNullExceptionsemaphoreSecurity is null.
T:System.UnauthorizedAccessExceptionThe user does not have F:System.Security.AccessControl.SemaphoreRights.ChangePermissions rights.-or-The semaphore was not opened with F:System.Security.AccessControl.SemaphoreRights.ChangePermissions rights.
T:System.NotSupportedExceptionThe current T:System.Threading.Semaphore object does not represent a named system semaphore.

Definition at line 355 of file Semaphore.cs.

◆ TryOpenExisting() [1/2]

static bool System.Threading.Semaphore.TryOpenExisting ( string  name,
out Semaphore  result 
)
static

Opens the specified named semaphore, if it already exists, and returns a value that indicates whether the operation succeeded.

Parameters
nameThe name of the system semaphore to open.
resultWhen this method returns, contains a T:System.Threading.Semaphore object that represents the named semaphore if the call succeeded, or null if the call failed. This parameter is treated as uninitialized.
Returns
true if the named semaphore was opened successfully; otherwise, false.
Exceptions
T:System.ArgumentExceptionname is an empty string.-or- name is longer than 260 characters.
T:System.ArgumentNullExceptionname is null.
T:System.IO.IOExceptionA Win32 error occurred.
T:System.UnauthorizedAccessExceptionThe named semaphore exists, but the user does not have the security access required to use it.

Definition at line 242 of file Semaphore.cs.

◆ TryOpenExisting() [2/2]

static bool System.Threading.Semaphore.TryOpenExisting ( string  name,
SemaphoreRights  rights,
out Semaphore  result 
)
static

Opens the specified named semaphore, if it already exists, with the desired security access, and returns a value that indicates whether the operation succeeded.

Parameters
nameThe name of the system semaphore to open.
rightsA bitwise combination of the enumeration values that represent the desired security access.
resultWhen this method returns, contains a T:System.Threading.Semaphore object that represents the named semaphore if the call succeeded, or null if the call failed. This parameter is treated as uninitialized.
Returns
true if the named semaphore was opened successfully; otherwise, false.
Exceptions
T:System.ArgumentExceptionname is an empty string.-or- name is longer than 260 characters.
T:System.ArgumentNullExceptionname is null.
T:System.IO.IOExceptionA Win32 error occurred.
T:System.UnauthorizedAccessExceptionThe named semaphore exists, but the user does not have the security access required to use it.

Definition at line 261 of file Semaphore.cs.


The documentation for this class was generated from the following file: