SpinLock.TryEnter Method (Int32, Boolean)

.NET Framework (current version)
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public void TryEnter(
	int millisecondsTimeout,
	ref bool lockTaken
)

Parameters

millisecondsTimeout
Type: System.Int32

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

lockTaken
Type: System.Boolean

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

Exception Condition
ArgumentOutOfRangeException

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

ArgumentException

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

LockRecursionException

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

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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: