Does the code not iterate throughout the Dictionary?

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!

+4
1

amountLeft = item.Value; ().

, lock.

:

static Dictionary<string, int> dict = new Dictionary<string, int>();
static object lockObject = new Object();

public static void IterateOverEachUser()
{
    lock (lockObject)
    {
        if (dict.Count > 0)
        {
            foreach (KeyValuePair<string, int> item in dict.ToList())
            {
                string userName = item.Key;
                int amountLeft = item.Value;
                if (amountLeft == 60)
                {
                    Console.WriteLine(userName + " started!");
                }
                Console.WriteLine(userName + amountLeft);
                dict[userName] = dict[userName] - 1;
                amountLeft = dict[userName];
                if (amountLeft == 0)
                {
                    Console.WriteLine(userName + " ran out!");
                }

                Console.WriteLine("User " + item.Key + " = " + amountLeft);
            }
        }
    }
}

public static void AddUser(string User)
{
    if (dict.ContainsKey(User))
    {
        Console.WriteLine("User already exists.");
    }
    else
    {
        dict.Add(User, 60);
        Console.WriteLine("User has been added.");
    }
}

static void Main(string[] args)
{

    AddUser("U1");
    AddUser("U2");

    int counter = 1;
    System.Timers.Timer t1 = new System.Timers.Timer();
    t1.Interval = 5000;
    t1.Elapsed += (oo, ee) => 
    {
        IterateOverEachUser();

        if (counter++ == 5)
            AddUser("U3");
    };

    t1.Start();

    Console.ReadKey();
}

:

User has been added.
User has been added.
U1 started!
U160
User U1 = 59
U2 started!
U260
User U2 = 59
U159
User U1 = 58
U259
User U2 = 58
U158
User U1 = 57
U258
User U2 = 57
U157
User U1 = 56
U257
User U2 = 56
U156
User U1 = 55
U256
User U2 = 55
User has been added.
U155
User U1 = 54
U255
User U2 = 54
U3 started!
U360
User U3 = 59
U154
User U1 = 53
U254
User U2 = 53
U359
User U3 = 58
U153
User U1 = 52
U253
User U2 = 52
U358
User U3 = 57
// and so one
+4

All Articles