/// <summary> /// The thread of the keeper /// </summary> protected override void ThreadWork() { while (true) { for (int i = 0; i < Fences.Length; i++) { Turd turdToClean = null; Fence curFence = Fences[i]; Monitor.Enter(curFence.TurdBuffer.Lock); try { if (curFence.TurdBuffer.IsBufferFull) { Monitor.Wait(curFence.TurdBuffer.Lock); } turdToClean = curFence.TurdBuffer.RemoveItem(); } finally { Monitor.Exit(curFence.TurdBuffer.Lock); } if (turdToClean != null) { Thread.Sleep(turdToClean.TimeToClean); TurdCleanedHandler?.Invoke(this, turdToClean); } } } }
/// <summary> /// What happens when a animal shits /// </summary> /// <param name="animal"></param> /// <param name="turd"></param> private void AnimalShitMethod(Animal.Animal animal, Turd turd) { Monitor.Enter(TurdBuffer.Lock); try { if (TurdBuffer.IsBufferFull) { Monitor.PulseAll(TurdBuffer.Lock); } TurdBuffer.AddItem(turd); if (TurdBuffer.CurrentItems == 1) { Monitor.PulseAll(TurdBuffer.Lock); } } finally { Monitor.Exit(TurdBuffer.Lock); } }