I use the dictionary to store user data. A timer is started that checks whether the user is online (how long ago did the user send a control packet saying that he is online).
A couple pops up an error
Value cannot be null. Parameter name: key
I can not understand what it is connected with.
Here is the code itself:
private void TimerFunc(object stat) { lock(this){ for (int i = 0; i < userTable.table.Count; i++) { try { var ud = userTable.table.ElementAt(i); if(ud.Key == null || ud.Value == null) { continue;//думал, что это как-то решит проблему } if (ud.Value.ip + ":" + ud.Value.port != serverData.IEPeP.ToString())//server не присылает нам пакет об онлайне, считается, что он онлайн всегда { Debug.WriteLine(ud.Value.ip + ":" + ud.Value.port); ud.Value.onlineTime -= 1000;//таймер запускает цикл каждую секунду if (ud.Value.onlineTime <= 0) { if(nickTable.table.ContainsKey(ud.Value.nick)) nickTable.table.Remove(ud.Value.nick); if(connectTable.table.ContainsKey(ud.Value.ip + ":" + ud.Value.port)) connectTable.table.Remove(ud.Value.ip + ":" + ud.Value.port); if(userTable.table.ContainsKey(ud.Key)) userTable.table.Remove(ud.Key); Debug.WriteLine("USER " + ud.Value.nick + " OFFLINE"); } else { userTable.table[ud.Key].onlineTime = ud.Value.onlineTime; } } } catch (Exception obj) { Debug.WriteLine("POINT 31+231: " + obj.Message);//вызывается это исключение } } } }
userTableis aDictionaryor another collection that usesKeyValuePairas elements, then everything is correct,KeyinKeyValuePaircannot benullunder any conditions - rdorn