Why the "Pause" button is not working in this Stopwatch timer? --Windows Phone App Developement

1.2k views Asked by At

I don't know why this pause button is not working here. Also my stopwatch class doesn't have Restart method so I thought to write it by combining "reset and start". Any other idea? Or any idea about how to make this pause button work?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using System.Diagnostics;

namespace PhoneApp2
{
    public partial class MainPage : PhoneApplicationPage
    {
        Stopwatch sw = new Stopwatch();
        DispatcherTimer newTimer = new DispatcherTimer();

        enum TimerState
        {
            Unknown,
            Stopped,
            Paused,
            Running
        }

        private TimerState _currentState = TimerState.Unknown;

        public MainPage()
        {
            InitializeComponent();

            newTimer.Interval = TimeSpan.FromMilliseconds(1000 / 30);
            newTimer.Tick += OnTimerTick;
        }


        void OnTimerTick(object sender, EventArgs args)
        {
            UpdateUI();
        }

        private void Button_Stop(object sender, RoutedEventArgs e)
        {
            Stop();
        }

        private void Button_Pause(object sender, RoutedEventArgs e)
        {
            Pause();
        }

        private void Button_Start(object sender, RoutedEventArgs e)
        {


            Start();

        }
        void UpdateUI()
        {
            textClock.Text = sw.ElapsedMilliseconds.ToString("0.00");
        }


        void Start()
        {
            sw.Reset();
            sw.Start();
            newTimer.Start();
            UpdateUI();
        }

        void Stop()
        {
            _currentState = TimerState.Stopped;
            sw.Stop();
            newTimer.Stop();
            UpdateUI();
        }

        void Pause()
        {
            _currentState = TimerState.Paused;
            sw.Stop();
            newTimer.Stop();
            UpdateUI();
        }

        void Resume()
        {
            if (_currentState == TimerState.Stopped)
            {
                sw.Reset();
            }
            _currentState = TimerState.Running;
            sw.Start();
            newTimer.Start();
            UpdateUI();
        }
    }
}

Thanks. P.S.: My .NET version is "Version 4.5.50709" Microsoft Visual Studio Express 2012 for Windows Phone. According to this LINK we should have a Restart method in Stopwatch class but mine doesn't have one however!

2

There are 2 answers

7
Ergwun On

If you're targetting Windows Phone, you're probably using Silverlight rather than .NET 4.5, and that would explain why your Stopwatch class does not have a Restart method.

In Silverlight, Stopwatch.Start() will start or resume measuring elapsed time for an interval.

The logic in your Start, Stop, Pause and Resume methods looks ok(*), but you only have event handlers for Button_Start, Button_Stop and Button_Pause. There is no Button_Resume.

Do you have a resume button? If not, where do you expect your Resume method to be called? Maybe you have hooked up a resume button to the Button_Start handler, which will reset your stopwatch?

If you have no Resume button, and you want the Start button to act resume after pausing and restart after stopping, then just change your Start button's click even handler to call Resume() instead of Start().

private void Button_Start(object sender, RoutedEventArgs e)
{
    Resume();
}

(*) However, you might want to disable Stop/Pause when the stopwatch is not running, as you can press stop, then pause and then when you press start, the timer will resume rather than restart etc.

2
Oberdan Nunes On

In order to pause/resume the DispatcherTimer, you need to use the .IsEnabled property. The reason Restart methods lack its related of the Framework you are using. This is not the full .Net Framework, just a sub-set of it. Exactly that @Ergwun said.