protected virtual Yield Invoke() { EventItem eventItem; if (filter.Receive(out eventItem)) { switch ((EventType)eventItem.EventType) { case EventType.Shutdown: Dispose(); filter.Pop(); break; default: throw new ApplicationException("Unexpected event: " + eventItem); } } var result = Yield.NoWork.Repeat; try { if (writeQueue.Count == 0) { return(result); } while (writeQueue.Count > 0) { if (!writeQueue.TryPeek(ref tick)) { break; } tickIO.Inject(tick); if (tickFile.TryWriteTick(tickIO)) { writeQueue.TryDequeue(ref tick); } result = Yield.DidWork.Repeat; } return(result); } catch (QueueException ex) { if (ex.EntryType == EventType.Terminate) { log.Notice("Last tick written: " + tickIO); if (debug) { log.Debug("Exiting, queue terminated."); } Finalize(); return(Yield.Terminate); } else { Exception exception = new ApplicationException("Queue returned unexpected: " + ex.EntryType); writeQueue.SetException(exception); writeQueue.Dispose(); Dispose(); throw ex; } } catch (Exception ex) { writeQueue.SetException(ex); writeQueue.Dispose(); Dispose(); throw; } }