Cgw.Service.SessionManage.timeoutTimer_Elapsed C# (CSharp) Method

timeoutTimer_Elapsed() private method

定时清理过期会话
private timeoutTimer_Elapsed ( object sender, System e ) : void
sender object
e System
return void
        private void timeoutTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timeoutTimer.Stop();

            if (sessionDict != null && sessionDict.Count > 0 &&
                rwl.TryEnterUpgradeableReadLock(CgwConst.ENTER_LOCK_WAIT_TIME))
            {
                List<CgwSession> deletingCGMSession = new List<CgwSession>();

                try
                {
                    foreach (CgwSession session in sessionDict.Values)
                    {
                        if (session != null
                            && (e.SignalTime - session.LastVisitedTime).TotalSeconds > sessionTimeout)
                        {
                            deletingCGMSession.Add(session);
                        }

                    }
                    //清除已过期的会话
                    if (deletingCGMSession.Count > 0)
                    {
                        try
                        {
                            if (rwl.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME))
                            {
                                CgwSession tempSession;
                                for (int i = 0; i < deletingCGMSession.Count; i++)
                                {
                                    tempSession = deletingCGMSession[i];
                                    if (tempSession != null)
                                    {
                                        //删除会话
                                        sessionDict.Remove(tempSession.SessionGuid);

                                        tempSession = null;
                                    }

                                }
                            }
                        }
                        finally
                        {
                            rwl.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    rwl.ExitUpgradeableReadLock();
                }
            }

            timeoutTimer.Start();
        }