/// <summary> /// Create a stage program. /// </summary> /// <param name="clock">Clock for timing</param> /// <param name="stageProvider">Stage provider that providing stages</param> /// <param name="preferPreloaded">Prefer pre-loaded stages if possible</param> public StageProgram([NotNull] IClock clock, [NotNull] IStageProvider stageProvider, bool preferPreloaded = true) { if (stageProvider == null) { throw new ArgumentNullException(nameof(stageProvider)); } OriginalClock = clock ?? throw new ArgumentNullException(nameof(clock)); FreezableClock = new FreezableClock(clock.As(TimeUnit.Millisecond)); FreezableClock.Frozen += (sender, e) => _pausedEvent.Set(); FreezableClock.Unfrozen += (sender, e) => _pausedEvent.Reset(); _stageProvider = preferPreloaded && stageProvider.TryPreload(out var preloaded) ? preloaded : stageProvider; _cyclicExecutor = new AsyncCyclicExecutor("StageProgram Worker", DoWork, true, ThreadPriority.Highest, null, StoppingAction.Abort); _cyclicExecutor.Starting += (sender, e) => { CurrentStage = null; _pausedEvent.Set(); StartTime = Time; _nextUpdateTime = 0; }; _cyclicExecutor.Started += (sender, e) => Started?.Invoke(this, EventArgs.Empty); _cyclicExecutor.Stopped += (sender, e) => Stopped?.Invoke(this, EventArgs.Empty); _stageSkipped = new AtomicBool(false); _pausedEvent = new ManualResetEvent(false); }