I am stumbling upon a problem, and I have no clue about what I'm doing wrong. I tried countless different things, but for some reason it 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 am looping IterateOverEachUser() every 5 seconds. When I add a user using the method, everything is fine, but when I add a second one, his value is stuck at 60 while the other one keeps rolling.
Does anyone knows why this happens? I'm coming from Java using HashMaps and using the same code there works as intended. (Which is: every user gets iterated over, the value of all users get deducted by 1 and then it stops until the IterateOverEachUser() method gets called again by the 5 second loop).
Thanks in advance!