SpinLock.TryEnter Method

Definition

Overloads

TryEnter(Boolean)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

TryEnter(Int32, Boolean)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

TryEnter(TimeSpan, Boolean)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

TryEnter(Boolean)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

public void TryEnter (ref bool lockTaken);
Parameters
lockTaken
Boolean

True if the lock is acquired; otherwise, false. lockTaken must be initialized to false prior to calling this method.

Exceptions

The lockTaken argument must be initialized to false prior to calling TryEnter.

Thread ownership tracking is enabled, and the current thread has already acquired this lock.

Remarks

Unlike Enter, TryEnter will not block waiting for the lock to be available. If the lock is not available when TryEnter is called, it will return immediately without any further spinning.

TryEnter(Int32, Boolean)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

public void TryEnter (int millisecondsTimeout, ref bool lockTaken);
Parameters
millisecondsTimeout
Int32

The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

lockTaken
Boolean

True if the lock is acquired; otherwise, false. lockTaken must be initialized to false prior to calling this method.

Exceptions

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

The lockTaken argument must be initialized to false prior to calling TryEnter.

Thread ownership tracking is enabled, and the current thread has already acquired this lock.

Remarks

Unlike Enter, TryEnter will not block indefinitely waiting for the lock to be available. It will block until either the lock is available or until the millisecondsTimeout has expired.

TryEnter(TimeSpan, Boolean)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

public void TryEnter (TimeSpan timeout, ref bool lockTaken);
Parameters
timeout
TimeSpan

A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

lockTaken
Boolean

True if the lock is acquired; otherwise, false. lockTaken must be initialized to false prior to calling this method.

Exceptions

timeout is a negative number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater than MaxValue milliseconds.

The lockTaken argument must be initialized to false prior to calling TryEnter.

Thread ownership tracking is enabled, and the current thread has already acquired this lock.

Remarks

Unlike Enter, TryEnter will not block indefinitely waiting for the lock to be available. It will block until either the lock is available or until the timeout has expired.