日期:2014-05-18 浏览次数:20563 次
if (Application["Users"] == null) { ht.Add(Convert.ToInt32(sdr["UserId"]), users); Application.Lock(); Application["Users"] = ht; Application.UnLock(); } else { ht = Application["Users"] as Hashtable; ArrayList a = new ArrayList(); if (ht[Convert.ToInt32(sdr["UserId"])] == null) { ht.Add(Convert.ToInt32(sdr["UserId"]), users); Application.Lock(); Application["Users"] = ht; Application.UnLock(); } }
------解决方案--------------------
楼上的看着有些晕,
public class OnlineManage
{
private static OnlineManage _instance = new OnlineManage();
//private Dictionary<int, OnlineUserInfo> _onlineUserList = new Dictionary<int, OnlineUserInfo>();
private Dictionary<string, OnlineUserInfo> _onlineSession = new Dictionary<string, OnlineUserInfo>();
public const int Timeout_Minutes = 20; //非登录会员在线用户过期时间
public const int Timeout_Minutes_Member = 40; //登录会员在线过期时间
private OnlineManage()
{
}
public void Clear()
{
lock(this)
{
//_onlineUserList.Clear();
_onlineSession.Clear();
}
}
/// <summary>
/// 获取当前在线用户总数(包含登录和未登录的)
/// </summary>
public int Count
{
get { return _onlineSession.Count; }
}
/// <summary>
/// 在线会员数
/// </summary>
public int MemberCount
{
get
{
int iRet = 0;
lock(this)
{
foreach (string _sKey in _onlineSession.Keys)
{
OnlineUserInfo oInfo = _onlineSession[_sKey];
if (oInfo == null)
{
_onlineSession.Remove(_sKey);
}
else
{
if (oInfo.UserID>0)
{
iRet++;
}
}
}
}
return iRet;
}
}
public OnlineUserInfo this[int UserID]
{
get
{
lock(this)
{
foreach (string _sKey in _onlineSession.Keys)
{
OnlineUserInfo oInfo = _onlineSession[_sKey];
if (oInfo == null)
{
_onlineSession.Remove(_sKey);
}
else
{
if (oInfo.UserID == UserID)
{
return oInfo;
}
}
}
return null;
}
}
}
public OnlineUserInfo this[string sSessionID]
{
get
{
if (_onlineSession.ContainsKey(sSessionID))
return _onlineSession[sSessionID];
else