I am trying to understand why this program doesn\'t work
Expected output: numbers 0-19 in random order What I get when I run: some numbers repeat, sometimes 20 is pr
You should just use the TPL, its a lot easier and recommended over manual thread management:
Parallel.For(0, 20, x => {
Thread.Sleep(1000);
Console.WriteLine("IN callback: The value I got is " + x);
});
This will also block until the loop finishes, if you don't want that you can use the TPL Task
, but I would definitely recommend avoiding threads.