dispatcher Timer "Jumps" when starting and stopping RRS feed

  • Question

  • I am using a dispatcher timer as a game timer. It worked great until I implemented pause and resume.

    I can stop the timer fine, but when i resume, it jumps ahead due to the change in time that occurs while the timer isn't running.

    how can I fix this, so that the timer stays in time?

    This is how I initialize it

    gameTimer = new DispatcherTimer();
    gameTimer.Interval = TimeSpan.FromSeconds(1);
    gameTimer.Tick += gameTimer_Tick;
    startTime = DateTime.Now;

    and this is what the Tick event does.

     currentGameWindow.GameTimeblk.Text = "Game Time: " + (DateTime.Now - startTime).ToString("hh':'mm':'ss");

    Saturday, February 7, 2015 8:17 AM


  • Hi.

    you need to store the time you pause the timer at

    pauseStart = DateTime.Now;
    And then when you continue you need to update your timer start time with the difference between the pause and continue time.
    pauseEnd = DateTime.Now;
    startTime = startTime.Add(pauseEnd - pauseStart);

    Saturday, February 7, 2015 10:26 AM