foreach(BruteforceEntry be in Entries.Values)
{
if (be.AddedTimeRemove <= now)
Entries.Remove(be.IPAddress);
else if (be.Unbantime <= now && be.Unbantime.Day == DateTime.Now.Day)
Entries.Remove(be.IPAddress);
}
An exception was thrown:
Collection was modified; enumeration operation may not execute.
For some reason, it is not any more.
I know you cannot remove something, while iterating through it this way. My question is: How do I solve it?
You can’t modify a collection you’re iterating over. In this case, a much better solution would be to create a list of entries to remove by iterating over the dictionary, and then iterate over that list, removing the entries from the dictionary:
Note that if you’re using .NET 3.5, you could use a LINQ query to express the first part: