void OnEnqueueTimerElapsed(Object O, ElapsedEventArgs EventArguments) { if (PatientsLeftCount == 0) { _EnqueueTimer.Elapsed -= new ElapsedEventHandler(OnEnqueueTimerElapsed); return; } _RandomSourceLocker.WaitOne(); var AddingPatient = _RandomSource.Next(10) < 4; _RandomSourceLocker.ReleaseMutex(); if (AddingPatient) { var NewPatientInQueue = HumanFactory.SpawnPatient(); PatientsLeftCount--; _QueueLocker.WaitOne(); _Queue.Add(NewPatientInQueue); QueueChanged(_Queue); _QueueLocker.ReleaseMutex(); _HistoryLocker.WaitOne(); HistoryUpdated($"Patien {NewPatientInQueue} got in queue."); _HistoryLocker.ReleaseMutex(); Thread.Sleep(10); } }