I recently worked on several multithreaded console applications and wondered how to do this. I use this code to control the number of threads created by the application:
foreach(string s in File.ReadAllLines("file.txt")){ while (threads >= maxThreads) ; Thread t = new Thread(() => { threads++; //thread code - make network request using 's' Console.WriteLine("TEST"); threads--; Thread.CurrentThread.Abort(); }); t.start(); }
However, due to the while loop, the Console.WriteLine method in the created one is locked and does not appear until the next free stream.
Is there any way to prevent this loop from blocking the call to Console.WriteLine ?
EDIT - A reverse condition in a while loop.
rodit source share