Semaphore WaitOne not operating correctly

c#, multithreading, semaphore

Solution

You are miscontructing the semaphore. It should be `_pool = new Semaphore(3, 3);` Doing that will also eliminate the need to pass in a timeout parameter to `WaitOne()`.

The first parameter is the initial number of requests that may be granted before blocking, so passing 0 means any subsequent calls to `WaitOne()` will immediately block.

Problem

I have a Semaphore that suspose to limit to 3, however, this just keeps calling as many as it wants. I'm assuming it's because I use (1000). However, when I try just () it will never pass the WaitOne i'm not sure what to do here. ``` private static Semaphore _pool; _pool = new Semaphore(0, 3); var options = new ParallelOptions(); options.MaxDegreeOfParallelism = 1; Parallel.ForEach(urlTable.AsEnumerable(),options, drow => { using (var WCC = new MasterCrawlerClass()) { ActiveThreads++; _pool.WaitOne(1000); Console.WriteLine("Active Thread #: " + ActiveThreads); WCC.MasterCrawlBegin(drow); Console.WriteLine("Done Crawling a datarow"); ActiveThreads--; _pool.Release(); } }); ```

Original source