4

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!

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • What behaviour are you seeing? Do your Stop and Resume buttons work? – Ergwun Sep 10 '13 at 03:33
  • When I press pause and then press start, it starts from the beginning not from the time I pause it! However stop button works fine and when I press it and then press start, it starts from the beginning as it should. – Mona Jalal Sep 10 '13 at 03:48

2 Answers2

3

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.

Ergwun
  • 12,579
  • 7
  • 56
  • 83
  • so there's no way to make use of Restart method in Windows Phone SDK? – Mona Jalal Sep 10 '13 at 03:49
  • @MonaJalal, the Restart method is just a shortcut to the role process, like described: `Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time. **public void Restart();**` – Oberdan Nunes Sep 10 '13 at 04:08
  • @MonaJalal No, you can't use `Restart()`. You can just call `Reset()` then `Start()` instead. It' hard to see why you want `Restart()` anyway, unless you want to expose an explicit Restart button. – Ergwun Sep 10 '13 at 06:03
  • @Ergwun Actually I have used the combination of "reset" and "start" for acting as "restart" but as you see it's not working for "pause" button and always resets! – Mona Jalal Sep 10 '13 at 06:16
  • @MonaJalal Is your `Resume` method ever called? (See my edit to the answer above.) – Ergwun Sep 10 '13 at 06:22
  • @Ergwun The fact is that I want it to be in a way that when I press pause and then press start, it resume and when I press stop and then start it start from zero. Actually I want my start button to act as both "start" as well as "resume" buttons. – Mona Jalal Sep 10 '13 at 06:38
  • @MonaJalal Then just have you `Button_Start` method call `Resume()` instead of `Start()`. You can delete the `Start()` method, as `Resume()` already has the correct logic for this. – Ergwun Sep 10 '13 at 07:41
1

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.

Oberdan Nunes
  • 509
  • 4
  • 13
  • And try to figure out if you aren't calling the Reset method. I've made a simple test here, and the Stopwatch Stop/Start methods worked fine. Without resetting the elapsed time. – Oberdan Nunes Sep 10 '13 at 04:03
  • 1
    You can put a breakpoint in the lines you are calling the `Reset()` method and check when it is called. Hope it can help. – Oberdan Nunes Sep 10 '13 at 04:23