private bool Take(bool waitForSemaphore) { bool gotSemaphore = false; do { protectCountMutex.WaitOne(); if (count < MaxCount) { count++; protectCountMutex.ReleaseMutex(); gotSemaphore = true; SemaphoreCountChanged?.Invoke(this, new SemaphoreCountChangedEventArgs(count)); // Log.Verbose("Counting Semaphore Take: Count={count}", count); } else { protectCountMutex.ReleaseMutex(); resetEvent.WaitOne(); } } while (!gotSemaphore && waitForSemaphore); return(gotSemaphore); }
public void Give() { protectCountMutex.WaitOne(); if (count > 0) { count--; //Log.Verbose("Counting Semaphore Give: Count={count}", count); resetEvent.Set(); SemaphoreCountChanged?.Invoke(this, new SemaphoreCountChangedEventArgs(count)); } protectCountMutex.ReleaseMutex(); }