Search code examples
c#lockingreaderwriterlockslim

Difference between EnterWriteLock and TryEnterWriteLock(Timeout.Infinite)


i'm a bit confused on the differences between EnterWriteLock() and TryEnterWriteLock() with Timeout.Infinite as parameter of ReaderWriterLockSlim. What is the point of giving an infinite timeout for entering the lock instead of using directly EnterWriteLock? What i have understood those two methods are exectly the same? What I am missing?


Solution

  • They're identical:

    public void EnterWriteLock()
    {
        TryEnterWriteLock(-1);
    }
    

    It's a bit easier to read the intent of lock.EnterWriteLock(), compared to lock.TryEnterWriteLock(-1) / lock.TryEnterWriteLock(Timeout.Infinite).