private void OnPlaybackTimerTick(object sender, EventArgs e) { DispatcherTimer timer = sender as DispatcherTimer; timer.Stop(); // Stop the timer before command completes. if (loadedCommands.Count <= 0) // There's nothing else for playback. { if (this.ExitAfterPlayback == false) { // The playback is done, but the command file indicates that // Dynamo should not be shutdown after the playback, so here // we simply invalidate the timer. // this.playbackTimer = null; } else { // The command file requires Dynamo be shutdown after all // commands has been played back. If that's the case, we'll // reconfigure the callback to a shutdown timer, and then // change its interval to the duration specified earlier. // this.playbackTimer.Tick -= OnPlaybackTimerTick; this.playbackTimer.Tick += OnShutdownTimerTick; var interval = TimeSpan.FromMilliseconds(PauseAfterPlayback); this.playbackTimer.Interval = interval; this.playbackTimer.Start(); // Start shutdown timer. } return; } // Remove the first command from the loaded commands. DynCmd.RecordableCommand nextCommand = loadedCommands[0]; loadedCommands.RemoveAt(0); // Execute the command, this may take a while longer than the timer // inverval (usually very short), that's why the timer was stopped // before the command execution starts. After the command is done, // the timer is then resumed for the next command in queue. // nextCommand.Execute(this.owningViewModel); timer.Start(); }
private void OnPlaybackTimerTick(object sender, EventArgs e) { DispatcherTimer timer = sender as DispatcherTimer; timer.Stop(); // Stop the timer before command completes. if (loadedCommands.Count <= 0) // There's nothing else for playback. { if (this.ExitAfterPlayback == false) { // The playback is done, but the command file indicates that // Dynamo should not be shutdown after the playback, so here // we simply invalidate the timer. // this.playbackTimer = null; ChangeStateInternal(State.Stopped); } else { // The command file requires Dynamo be shutdown after all // commands has been played back. If that's the case, we'll // reconfigure the callback to a shutdown timer, and then // change its interval to the duration specified earlier. // this.playbackTimer.Tick -= OnPlaybackTimerTick; this.playbackTimer.Tick += OnShutdownTimerTick; var interval = TimeSpan.FromMilliseconds(PauseAfterPlayback); this.playbackTimer.Interval = interval; this.playbackTimer.Start(); // Start shutdown timer. ChangeStateInternal(State.ShuttingDown); } return; } // Remove the first command from the loaded commands. DynCmd.RecordableCommand nextCommand = loadedCommands[0]; loadedCommands.RemoveAt(0); // Update the cached command references. this.PreviousCommand = this.CurrentCommand; this.CurrentCommand = nextCommand; if (nextCommand is DynCmd.PausePlaybackCommand) { var command = nextCommand as DynCmd.PausePlaybackCommand; PauseCommandPlayback(command.PauseDurationInMs); return; } try { // Execute the command, this may take a while longer than the timer // inverval (usually very short), that's why the timer was stopped // before the command execution starts. After the command is done, // the timer is then resumed for the next command in queue. // nextCommand.Execute(this.owningViewModel); } catch (Exception exception) { // An exception is thrown while playing back a command. Remove any // pending commands and allow the "playbackTimer" to continue with // its next tick. Proper shutdown sequence will be initialized // when the "playbackTimer" tries to pick up the next command and // realized that there is no more commands waiting. // loadedCommands.Clear(); this.PlaybackException = exception; } timer.Start(); }