SpinLock.TryEnter Method

Definition

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.

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)

Source:
SpinLock.cs
Source:
SpinLock.cs
Source:
SpinLock.cs

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

TryEnter(Int32, Boolean)

Source:
SpinLock.cs
Source:
SpinLock.cs
Source:
SpinLock.cs

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

TryEnter(TimeSpan, Boolean)

Source:
SpinLock.cs
Source:
SpinLock.cs
Source:
SpinLock.cs

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 Int32.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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0