Why does the Pause button not work in this stopwatch timer? - Application development for Windows Phone

I do not know why this pause button does not work here. Also, my stopwatch class does not have a reset method, so I decided to write it by combining "reset and start". Any other idea? Or any idea on 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. PS: My .NET version is "Version 4.5.50709" Microsoft Visual Studio Express 2012 for Windows Phone. According to this LINK , we should have the Restart method in the stopwatch class, but I don’t have it!

+4
source share
2 answers

If you use Windows Phone, you are probably using Silverlight, not .NET 4.5, and this explains 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 the Start , Stop , Pause and Resume methods looks fine (*), but you only have event handlers for Button_Start , Button_Stop and Button_Pause . No Button_Resume .

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

If you don't have a Resume button, and you want the Start button to resume after a pause and restart after a stop, simply change the Start button to the Only button to call Resume() instead of Start() .

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

(*) However, you can turn off Stop / Pause when the stopwatch is not working, since you can click the Stop button, then pause, and then when you press the Start button, the timer will resume rather than restart, etc.

+3
source

To pause / resume DispatcherTimer , you need to use the .IsEnabled property. The reason for the Restart method is not related to the Framework you are using. This is not a complete .NET Framework, just a subset of it. That is what @Ergwun said.

+1
source

Source: https://habr.com/ru/post/1501394/


All Articles