Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset a DispatcherTimer?

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

like image 802
kaza.ma Avatar asked Nov 27 '14 17:11

kaza.ma


1 Answers

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

like image 79
Chubosaurus Software Avatar answered Sep 27 '22 02:09

Chubosaurus Software