Redigera

Dela via


ReaderWriterLockSlim.TryEnterWriteLock Method

Definition

Tries to enter the lock in write mode, with an optional time-out.

Overloads

TryEnterWriteLock(Int32)

Tries to enter the lock in write mode, with an optional time-out.

TryEnterWriteLock(TimeSpan)

Tries to enter the lock in write mode, with an optional time-out.

TryEnterWriteLock(Int32)

Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs

Tries to enter the lock in write mode, with an optional time-out.

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

Parameters

millisecondsTimeout
Int32

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

Returns

true if the calling thread entered write mode, otherwise, false.

Exceptions

The RecursionPolicy property is NoRecursion and the current thread has already entered the lock.

-or-

The current thread initially entered the lock in read mode, and therefore trying to enter write mode would create the possibility of a deadlock.

-or-

The recursion number would exceed the capacity of the counter. The limit is so large that applications should never encounter it.

The value of millisecondsTimeout is negative, but it is not equal to Infinite (-1), which is the only negative value allowed.

The ReaderWriterLockSlim object has been disposed.

Examples

The following example shows how to use the TryEnterWriteLock method to enter the lock in write mode, with a time-out. The method shown in the example adds a new key/value pair to the synchronized cache. If the specified time-out interval elapses before the thread enters the lock, the method returns false. The method returns true if the key/value pair is added.

If the key is already in the cache, the exception thrown by the inner Dictionary<TKey,TValue> is allowed to terminate the method. A finally block is used to execute the ExitWriteLock method, ensuring that the caller exits the lock.

This code is part of a larger example provided for the ReaderWriterLockSlim class.

private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
private Dictionary<int, string> innerCache = new Dictionary<int, string>();
Private cacheLock As New ReaderWriterLockSlim()
Private innerCache As New Dictionary(Of Integer, String)
public bool AddWithTimeout(int key, string value, int timeout)
{
    if (cacheLock.TryEnterWriteLock(timeout))
    {
        try
        {
            innerCache.Add(key, value);
        }
        finally
        {
            cacheLock.ExitWriteLock();
        }
        return true;
    }
    else
    {
        return false;
    }
}
Public Function AddWithTimeout(ByVal key As Integer, ByVal value As String, _
                               ByVal timeout As Integer) As Boolean
    If cacheLock.TryEnterWriteLock(timeout) Then
        Try
            innerCache.Add(key, value)
        Finally
            cacheLock.ExitWriteLock()
        End Try
        Return True
    Else
        Return False
    End If
End Function

Remarks

If millisecondsTimeout is 0 (zero), this method checks the lock state and returns false immediately if the desired state is unavailable.

If other threads have entered the lock in read mode, a thread that calls the TryEnterWriteLock method blocks until those threads have exited read mode or until the time-out interval has elapsed. While threads are blocked waiting to enter write mode, additional threads that try to enter read mode or upgradeable mode block until all the threads waiting to enter write mode have either timed out or entered write mode and then exited from it.

Note

If a lock allows recursion, a thread that has entered the lock in write mode can enter write mode recursively, even if other threads are waiting to enter write mode.

Applies to

TryEnterWriteLock(TimeSpan)

Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs

Tries to enter the lock in write mode, with an optional time-out.

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

Parameters

timeout
TimeSpan

The interval to wait, or -1 milliseconds to wait indefinitely.

Returns

true if the calling thread entered write mode, otherwise, false.

Exceptions

The RecursionPolicy property is NoRecursion and the current thread has already entered the lock.

-or-

The current thread initially entered the lock in read mode, and therefore trying to enter write mode would create the possibility of a deadlock.

-or-

The recursion number would exceed the capacity of the counter. The limit is so large that applications should never encounter it.

The value of timeout is negative, but it is not equal to -1 milliseconds, which is the only negative value allowed.

-or-

The value of timeout is greater than Int32.MaxValue milliseconds.

The ReaderWriterLockSlim object has been disposed.

Remarks

If timeout is 0 (zero), this method checks the lock state and returns false immediately if the desired state is unavailable.

If other threads have entered the lock in read mode, a thread that calls the TryEnterWriteLock method blocks until those threads have exited read mode or until the time-out interval has elapsed. While threads are blocked waiting to enter write mode, additional threads that try to enter read mode or upgradeable mode block until all the threads waiting to enter write mode have either timed out or entered write mode and then exited from it.

Note

If a lock allows recursion, a thread that has entered the lock in write mode can enter write mode recursively, even if other threads are waiting to enter write mode.

Applies to