15

Those are my declarations and methods of DispatcherTimer:

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;
}        

This is where 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 app and i want everytime the user press capture to show a timer. My problem here is that i can't reset it

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
kazama
  • 211
  • 1
  • 2
  • 10

2 Answers2

25

Calling Stop() and then Start() should restart the Timer Interval.

ispiro
  • 26,556
  • 38
  • 136
  • 291
Chubosaurus Software
  • 8,133
  • 2
  • 20
  • 26
  • 12
    You don't need to call `Stop()`, calling `Start()` will be enough to reset the timer. – Romasz Nov 27 '14 at 17:50
  • True, but it doesn't make sense reading the code like that. That is an assumption (shortcut) lazy programmers make and will be weird to anyone new reading the code. – Chubosaurus Software Nov 27 '14 at 18:02
  • 1
    where exactly call `Stop()` ? i tried between `InitTimer()` and `Start()` but it continues counting from where it stopped. – kazama Nov 27 '14 at 18:47
  • 2
    @KazaMaster That is because of another problem. Your math is incorrect. You never reset SpanTime = 0, you keep adding to it... – Chubosaurus Software Nov 27 '14 at 18:51
  • You should use the Stopwatch class for the total elapsed time. Timers aren't very reliable and timer errors will add up. You'll still need a Timer to trigger the Tick, and can set the next interval based on the Stopwatch Elapsed time depending on how late the timer was to prevent timer errors compounding. – AndrewS Jun 28 '15 at 12:27
  • 5
    @Romasz To reset the timer, you need to call Stop() and Start() [.NET Reference Source: DispatcherTimer.Start()](https://referencesource.microsoft.com/#WindowsBase/Base/System/Windows/Threading/DispatcherTimer.cs,a3450586a3e54079) – marbel82 Mar 12 '18 at 14:41
  • @marbel82 Might depend on the framework version, I'm not sure now how it looked at WP8.1. – Romasz Mar 12 '18 at 15:59
  • ["Source"](https://github.com/MicrosoftDocs/winrt-api/issues/380) which is an answer to the `Closed` feedback on [Microsoft docs here](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.dispatchertimer.start). – ispiro Sep 14 '18 at 13:58
9

You need to (re)set your SpanTime when you press the Capture-button. just do

SpanTime = new TimeSpan();

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

chrosey
  • 220
  • 3
  • 15
  • 1
    i don't really understand this downvote. sure i didn't answer his question title. but in his description, it looks like he wants to reset the time that is shown for the user. so i supposed my answer is what he was looking for – chrosey Nov 27 '14 at 18:49