public void Pause() { if (this.State == TDTimerState.Running) { this.State = TDTimerState.Paused; } }
void TDTimer_Elapsed(object sender, ElapsedEventArgs e) { // the timer was running, and the duration elapsed. if (this.State == TDTimerState.Running) { this.State = TDTimerState.Finished; } }
private void HandleStateChange(TDTimerState oldState, TDTimerState newState) { // if same state, ignore if (oldState == newState) { return; } if (oldState == TDTimerState.Finished) { return; } // never switch away from finished. // otherwise do something based on the new state switch (newState) { case TDTimerState.Finished: // timer has finished break; case TDTimerState.Paused: // user has paused the game, so pause this timer PauseWhen = DateTime.Now; //base.Enabled = false; break; case TDTimerState.Running: if (oldState == TDTimerState.NotStartedYet) { // starting for first time } else if (oldState == TDTimerState.Paused) { // set the remaining duration because the timer starts over this.Duration = new TimeSpan(0, 0, 0, 0, (int)Math.Max(0, DurationRemaining.TotalMilliseconds)); } // and set the resumeWhen time for future state changes ResumeWhen = DateTime.Now; break; case TDTimerState.NotStartedYet: // should never get here default: break; } }
public void Resume() { if (this.State == TDTimerState.NotStartedYet) { base.Start(); if (ResumeWhen == new DateTime()) { ResumeWhen = DateTime.Now; } this.State = TDTimerState.Running; } else if (this.State == TDTimerState.Paused) { this.State = TDTimerState.Running; } this.ChangeSpeedWhen = DateTime.Now; }