I'm trying to add multiple values to a ConcurrentBag, but no values actually get inside. At first I tried using List, but that apparently isn't "Thread-Safe", so I searched around and it seems like people suggest using ConcurrentBag. I tried using Thread.Sleep(100) with List and that worked, but it's slower. How can I properly add values? The debuger always shows "Count:0". Here's my code:
foreach (KeyValuePair<string, string> entry in test_Words)
{
Form1.fr.progressBar1.Value++;
new Thread(delegate () {
switch (test_Type)
{
case "Definitions":
bagOfExercises.Add(Read(Definitions.get(entry.Value, entry.Key)));
break;
case "Examples":
bagOfExercises.Add(Read(Examples.get(entry.Value, entry.Key)).Replace(entry.Key, new string('_', entry.Key.Length)));
break;
}
}).Start();
}