I have a while loop that I want to init threads however the below code ends up creating almost 50 threads. Far above my intended limit of 5 Not sure what I'm doing wrong.
public static SemaphoreSlim scraperSemaphore = new SemaphoreSlim(5);
private void startScraper()
{
while (Core.Scraper.UsernameQueue.Count > 0)
{
string uName = Core.Scraper.UsernameQueue.Dequeue();
scraperSemaphore.Wait();
Thread t = new Thread(() => Core.Scraper.scrapeFunction(uName));
t.Start();
scraperSemaphore.Release();
}
}