1
Answer

Repeated results when using multithread

wei1ec

wei1ec

20y
1.7k
1
Hi, I have two classes for this example, bascially I want to pass a variable to another class and print it out, and then keep updating it. I do it like this: class1: NewClass nc = new NewClass(); for (int i = 0; i < 100; i++) { nc.SetVariable = i.ToString(); Thread t = new Thread(new ThreadStart(nc.Run)); t.Start(); } class2 (name: NewClass): private string var; public string SetVariable { set { var = value; } } public void Run() { Console.WriteLine(var); } It'll repeat some values, and it doesn't even print out all the numbers 0-99... Why is this happening? How can I fix it? Thank you for reading!
Answers (1)