InterprocessLockGetNamedSemaphore(Boolean, Int32, Int32) Method

Gets a uniquely named inter-process NamedSemaphore associated with the running application, typically used to detect whether some number of instances of the application are already running.

Definition

Namespace: Gemstone.Threading
Assembly: Gemstone.Common (in Gemstone.Common.dll) Version: 1.0.128 -- Release Build+d050cfc5563c89a1188cc3c6b2d417530856f490
public static NamedSemaphore GetNamedSemaphore(
	bool perUser,
	int maximumCount = 10,
	int initialCount = -1
)

Parameters

perUser  Boolean
Indicates whether to generate a different name for the NamedSemaphore dependent upon the user running the application.
maximumCount  Int32  (Optional)
The maximum number of requests for the semaphore that can be granted concurrently.
initialCount  Int32  (Optional)
The initial number of requests for the semaphore that can be granted concurrently, or -1 to default to maximumCount.

Return Value

NamedSemaphore
A uniquely named inter-process NamedSemaphore specific to entry assembly; NamedSemaphore is created if it does not exist.

Remarks

This function uses a hash of the assembly's GUID when creating the NamedSemaphore, if it is available. If it is not available, it uses a hash of the simple name of the assembly. Although the name is hashed to help guarantee uniqueness, it is still entirely possible that another application may use that name with the same hashing algorithm to generate its NamedSemaphore name. Therefore, it is best to ensure that the GuidAttribute is defined in the AssemblyInfo of your application.

On POSIX systems, the NamedSemaphore exhibits kernel persistence, meaning instances will remain active beyond the lifespan of the creating process. Named semaphores must be explicitly removed by invoking Unlink when they are no longer needed. Kernel persistence necessitates careful design consideration regarding the responsibility for invoking Unlink. Since the common use case for named semaphores is across multiple applications, it is advisable for the last exiting process to handle the cleanup. In cases where an application may crash before calling Unlink, the semaphore persists in the system, potentially leading to resource leakage. Implementations should include strategies to address and mitigate this risk.

Exceptions

UnauthorizedAccessExceptionThe named semaphore exists, but the user does not have the minimum needed security access rights to use it.

See Also