I am facing a problem and I do not know what I am doing wrong. I tried a lot of different things, but for some reason this just won't work. My main loop:
static Dictionary<string, int> dict = new Dictionary<string, int>();
public static void IterateOverEachUser()
{
if (dict.Count > 0) {
foreach (KeyValuePair<string, int> item in dict.ToList())
{
string userName = item.Key;
int amountLeft = item.Value;
if(amountLeft == 60)
{
Log(userName + " started!");
}
Log(userName + amountLeft);
dict[userName] = dict[userName] - 1;
amountLeft = item.Value;
if(amountLeft == 0)
{
Log(userName + " ran out!");
}
}
}
}
public static void AddUser(string User)
{
if (dict.ContainsKey(User))
{
Log("User already exists.");
}
else
{
dict.Add(User,60);
Log("User has been added.");
}
}
I execute an IterateOverEachUser () loop every 5 seconds. When I add a user using this method, everything is fine, but when I add a second, its value is stuck at 60, and the other continues to roll.
Does anyone know why this is happening? I come from Java using HashMaps, and using the same code that works as intended. (Which: each user gets an iteration, the value of all users is subtracted by 1, and then stops until the IterateOverEachUser () method is called again by a 5 second loop).
Thanks in advance!