System.Threading.LockExecution

public class LockExecution

Class Overview

Provides utilities for executing code sections under exclusive lock, ensuring thread safety.

Methods

Execute<TResult>(Func<TResult> function, TimeSpan timeout)

Syntax:
public static TResult Execute<TResult>(Func<TResult> function, TimeSpan timeout)

Executes the provided function within a lock, waiting for the specified timeout if the lock is unavailable.

If the lock cannot be acquired within the timeout, an exception is thrown.

Parameters:

  • function: The function to execute under the lock.
  • timeout: The maximum time to wait for the lock.

Returns: The result of the executed function.

Throws: TimeoutException if the lock cannot be acquired within the specified timeout.

Execute(Action action, TimeSpan timeout)

Syntax:
public static void Execute(Action action, TimeSpan timeout)

Executes the provided action within a lock, waiting for the specified timeout if the lock is unavailable.

If the lock cannot be acquired within the timeout, an exception is thrown.

Parameters:

  • action: The action to execute under the lock.
  • timeout: The maximum time to wait for the lock.

Throws: TimeoutException if the lock cannot be acquired within the specified timeout.

Execute(Action action)

Syntax:
public static void Execute(Action action)

Executes the provided action within a lock, blocking indefinitely until the lock is available.

Parameters:

  • action: The action to execute under the lock.