Opc.Ua.SafeLock.TryEnter C# (CSharp) Method

TryEnter() public method

Attempts to acquire the lock.
Thrown if the lock state is inconsistent.
public TryEnter ( int timeout ) : bool
timeout int The number of milliseconds to wait.
return bool
        public bool TryEnter(int timeout)
        {
            if (!System.Threading.Monitor.TryEnter(this, timeout))
            {
                return false;
            }

            if (m_refs == 0)
            {
                int result = Interlocked.CompareExchange(ref m_owner, Thread.CurrentThread.ManagedThreadId, -1);

                if (result != -1)
                {
                    throw new InvalidOperationException("Operation failed because Lock object is in an invalid state.");
                }
            }

            m_refs++;

            return true;
        }