Trying to make program run on list of urls and open limited number of threads to process them. Problem is that the while loop will execute when listPosition is 18, even that the siteUrls.Count is 18. But it will never show any of those test messageboxes i added. Thread code itself has a locker that does currentThreads-- in the end of thread's work.
Why does it start the while loop when the condition becomes (18<18)? Why does it try to launch thread with the 18 but it wont show a message box? Problem exists only when i put more threads than list items count. Its strange to me because i think it should not run the loop when listPosition equals _siteUrls.Count. I tried adding various "waiting" for threads to start and what not, but it still doesn't work.
p.s. when i add 5ms wait before increasing listPosition it becomes more stable. What exactly does it need to wait for so i can program it in more nicely?
p.p.s Thread doesn't operate any of the counters except doing lock (ThreadsMinus) { currentThreads--;}
listPosition = 0;
while (listPosition < siteUrls.Count)
{
if (listPosition == 18)
{
MessageBox.Show("18");
}
currentThreads++;
var t = new Thread(() => Register(siteUrls[listPosition], listPosition));
t.Start();
if (listPosition == 18)
{
MessageBox.Show("18");
}
while (currentThreads >= maxThreads)
{
Thread.Sleep(50);
}
listPosition++;
}