C# Class SimpleHelpers.NamedLock

Synchronization helper: a static lock collection associated with a key. NamedLock manages the lifetime of critical sections that can be accessed by a key (name) throughout the application. It also have some helper methods to allow a maximum wait time (timeout) to aquire the lock and safelly release it. Note: this nuget package contains c# source code and depends on System.Collections.Concurrent introduced in .Net 4.0.
Inheritance: IDisposable
Show file Open project: khalidsalomao/SimpleHelpers.Net Class Usage Examples

Public Methods

Method Description
CreateAndEnter ( string key ) : NamedLock

Creates a new instance and tries to aquire a lock.

CreateAndEnter ( string key, System.TimeSpan waitTimeout ) : NamedLock

Creates a new instance and tries to aquire a lock.

CreateAndEnter ( string key, int waitTimeoutMilliseconds ) : NamedLock

Creates a new instance and tries to aquire a lock.

Dispose ( ) : void

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. Releases aquired lock and related resources.

Enter ( ) : bool

Tries to aquire a lock.

Enter ( System.TimeSpan waitTimeout ) : bool

Tries to aquire a lock respecting the specified timeout.

Enter ( int waitTimeoutMilliseconds ) : bool

Tries to aquire a lock respecting the specified timeout.

Exit ( ) : bool

Releases the lock if it was already aquired. Called also at "Dispose".

NamedLock ( string key ) : System

Initializes a new instance of the NamedLock class.

Private Methods

Method Description
GetOrAdd ( string key ) : object
LockFactory ( string key ) : CountedLock
ReleaseOrRemove ( string key ) : void

Method Details

CreateAndEnter() public static method

Creates a new instance and tries to aquire a lock.
public static CreateAndEnter ( string key ) : NamedLock
key string The named lock key.
return NamedLock

CreateAndEnter() public static method

Creates a new instance and tries to aquire a lock.
public static CreateAndEnter ( string key, System.TimeSpan waitTimeout ) : NamedLock
key string The named lock key.
waitTimeout System.TimeSpan The wait timeout.
return NamedLock

CreateAndEnter() public static method

Creates a new instance and tries to aquire a lock.
public static CreateAndEnter ( string key, int waitTimeoutMilliseconds ) : NamedLock
key string The named lock key.
waitTimeoutMilliseconds int The wait timeout milliseconds.
return NamedLock

Dispose() public method

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. Releases aquired lock and related resources.
public Dispose ( ) : void
return void

Enter() public method

Tries to aquire a lock.
public Enter ( ) : bool
return bool

Enter() public method

Tries to aquire a lock respecting the specified timeout.
public Enter ( System.TimeSpan waitTimeout ) : bool
waitTimeout System.TimeSpan The wait timeout.
return bool

Enter() public method

Tries to aquire a lock respecting the specified timeout.
public Enter ( int waitTimeoutMilliseconds ) : bool
waitTimeoutMilliseconds int The wait timeout milliseconds.
return bool

Exit() public method

Releases the lock if it was already aquired. Called also at "Dispose".
public Exit ( ) : bool
return bool

NamedLock() public method

Initializes a new instance of the NamedLock class.
public NamedLock ( string key ) : System
key string The named lock key.
return System