Redigera

Dela via


Lock.TryEnter Method

Definition

Overloads

TryEnter()

Tries to enter the lock without waiting.

TryEnter(Int32)

Tries to enter the lock, waiting if necessary for the specified number of milliseconds until the lock can be entered.

TryEnter(TimeSpan)

Tries to enter the lock, waiting if necessary until the lock can be entered or until the specified timeout expires.

TryEnter()

Source:
Lock.cs

Tries to enter the lock without waiting.

public:
 bool TryEnter();
public bool TryEnter ();
member this.TryEnter : unit -> bool
Public Function TryEnter () As Boolean

Returns

true if the lock was entered by the current thread; otherwise, false.

Exceptions

The lock has reached the limit of repeated entries by the current thread. The limit is implementation-defined and is intended to be high enough that it would not be reached in normal situations.

Remarks

When the method returns true, the current thread is the only thread that holds the lock. If the lock can't be entered immediately, the method returns false without waiting for the lock. If the lock is already held by the current thread, the lock is entered again. To fully exit the lock and allow other threads to enter the lock, the current thread should exit the lock as many times as it has entered the lock.

For more information, see the Remarks for Lock.

Applies to

TryEnter(Int32)

Source:
Lock.cs

Tries to enter the lock, waiting if necessary for the specified number of milliseconds until the lock can be entered.

public:
 bool TryEnter(int millisecondsTimeout);
public bool TryEnter (int millisecondsTimeout);
member this.TryEnter : int -> bool
Public Function TryEnter (millisecondsTimeout As Integer) As Boolean

Parameters

millisecondsTimeout
Int32

The number of milliseconds to wait until the lock can be entered. Specify Timeout.Infinite (-1) to wait indefinitely, or 0 to not wait.

Returns

true if the lock was entered by the current thread; otherwise, false.

Exceptions

millisecondsTimeout is less than -1.

The lock has reached the limit of repeated entries by the current thread. The limit is implementation-defined and is intended to be high enough that it would not be reached in normal situations.

Remarks

When the method returns true, the current thread is the only thread that holds the lock. If the lock can't be entered immediately, the method waits until the lock can be entered or until the timeout specified by the millisecondsTimeout parameter expires. If the timeout expires before entering the lock, the method returns false. If the lock is already held by the current thread, the lock is entered again. To fully exit the lock and allow other threads to enter the lock, the current thread should exit the lock as many times as it has entered the lock.

For more information, see the Remarks for Lock.

Applies to

TryEnter(TimeSpan)

Source:
Lock.cs

Tries to enter the lock, waiting if necessary until the lock can be entered or until the specified timeout expires.

public:
 bool TryEnter(TimeSpan timeout);
public bool TryEnter (TimeSpan timeout);
member this.TryEnter : TimeSpan -> bool
Public Function TryEnter (timeout As TimeSpan) As Boolean

Parameters

timeout
TimeSpan

A TimeSpan that represents the number of milliseconds to wait until the lock can be entered. Specify a value that represents Timeout.Infinite (-1) milliseconds to wait indefinitely, or a value that represents 0 milliseconds to not wait.

Returns

true if the lock was entered by the current thread; otherwise, false.

Exceptions

timeout, after its conversion to an integer millisecond value, represents a value that is less than -1 milliseconds or greater than Int32.MaxValue milliseconds.

The lock has reached the limit of repeated entries by the current thread. The limit is implementation-defined and is intended to be high enough that it would not be reached in normal situations.

Remarks

When the method returns true, the current thread is the only thread that holds the lock. If the lock can't be entered immediately, the method waits until the lock can be entered or until the specified timeout expires. If the timeout expires before entering the lock, the method returns false. If the lock is already held by the current thread, the lock is entered again. To fully exit the lock and allow other threads to enter the lock, the current thread should exit the lock as many times as it has entered the lock.

For more information, see the Remarks for Lock.

Applies to