private void refreshNeigbors() { //lock must be used, since these variables can be accessed from Game1 methods lock (NeighborsTable) { NeighborsTable.Clear(); //NeighborsTable.AddRange(Program.NodeListuC.Where(x => IsNeigbor(x)).Select(x => x.NodeAddress)); foreach (var node in Program.NodeListuC) { if (IsNeigbor(node)) { NeighborsTable.Add(node.NodeAddress); } } } //remove from routeTable all the gateways that currently aren't neighbors lock (RouteTable) { foreach (var item in RouteTable.ToList()) { byte address = item.Key; byte gateway = item.Value[0]; if (!NeighborsTable.Contains(gateway))//gateway is not neighbor { RouteTable.Remove(address); } } } }