SpinLock.TryEnter Method (TimeSpan, Boolean)

.NET Framework 4.6 and 4.5

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
Assemblies:   System.Threading (in System.Threading.dll)
  mscorlib (in mscorlib.dll)

public void TryEnter(
	TimeSpan timeout,
	ref bool lockTaken
)

Parameters

timeout
Type: System.TimeSpan

A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds 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.

ExceptionCondition
ArgumentOutOfRangeException

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

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 timeout has expired.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft