Exemple #1
0
        internal void UnLock(MutiThreadDisposeState state)
        {
            Func <bool> ul = () =>
            {
                if (((IDisposeState)state).IsValid)
                {
                    if (state.IsWriteLock)
                    {
                        writeThreadCounts.RemoveThreadCount();
                    }
                    else
                    {
                        readThreadCounts.RemoveThreadCount();
                    }
                }
                return(true);
            };

            // 为保证线程安全,所有对锁的操作都应通过WaitForTime执行
            rwLock.WaitForTime(TimeSpan.MaxValue, ul);
        }
Exemple #2
0
        public IDisposeState LockWrite(TimeSpan timeout, Func <bool> isvalidstate)
        {
            IDisposeState state = null;
            Func <bool>   f     = () =>
            {
                if (dispose)
                {
                    state = DisposeState.Empty;
                    return(true);
                }
                if (TryLockWrite())
                {
                    bool isvalid = isvalidstate != null?isvalidstate() : true;

                    if (isvalid)
                    {
                        state = new MutiThreadDisposeState(isvalid, true, this);
                    }
                    else
                    {
                        state = DisposeState.Empty;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            };

            if (rwLock.WaitForTime(timeout, f))
            {
                return(state);
            }
            else
            {
                return(DisposeState.Empty);
            }
        }