private void Run() { while (_running) { try { var item = _queue.Take(); item.SetQueueLength(_queue.Count); item.MarkStarted(); if (item.Type == nameof(KillMessage)) { _running = false; } else if (item.Type == nameof(StatsMessage)) { var instrumentedMessageProcessor = _messageProcessor as IStatsHandler; instrumentedMessageProcessor?.OnStats(new WorkerStats(_stats.GetBatch())); } else { _messageProcessor.OnMessage(item.Message); } item.MarkDone(); var messageStat = item.GetStat(); _stats.Add(messageStat); } catch (Exception exception) { var shouldExit = _errorHandler.OnError(Name, exception); _running = !shouldExit; } } }