How to reset DispatcherTimer?

These are my declarations and DispatcherTimer methods:

private DispatcherTimer DishTimer; private TimeSpan SpanTime; private void InitTimer() { DishTimer = new DispatcherTimer(); DishTimer.Interval = new TimeSpan(0, 0, 0, 1); DishTimer.Tick += TimerOnTick; } private void TimerOnTick(object sender, object o) { SpanTime = SpanTime.Add(DishTimer.Interval); Duration.DataContext = SpanTime; } 

Here I call it:

 private void CaptureButton_Click(object sender, RoutedEventArgs e) { if ((string) CaptureButton.Content == "Capture") { CaptureAudio(); InitTimer(); DishTimer.Start(); ProgressRing.IsActive = true; CaptureButton.Content = "Stop"; } else { StopCapture(); DishTimer.Stop(); ProgressRing.IsActive = false; CaptureButton.Content = "Capture"; } } 

and here is my xaml code for showing the timer:

 <TextBlock Name="Duration" Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"></TextBlock> 

I am making a voice recording application, and I want to show a timer every time the user clicks the capture button. My problem here is that I cannot reset it

+7
source share
2 answers

When you click the Capture button, you must (re) install SpanTime . just

 SpanTime = new TimeSpan(); 

it should be reset to zero and start until you press the button again.

+3
source share

A call to Stop() and then Start() should restart the timer interval.

+13
source share

All Articles