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!