locked
Reset DispatcherTimer Object value

    Question

  • Hi all,

    I have started creating a audio recorder for Windows Store. To display the Duration of the audio being recorder I have used

    DispatcherTimer object of DispatcherTimer class. I want to reset the DispatcherTimer to initial value ( may be 0.0.0.0.0).

    My code is :-

     private void InitTimer()
            {
                DishTimer = new DispatcherTimer();
                DishTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
                DishTimer.Tick += TimerOnTick;
            }

            private void TimerOnTick(object sender, object o)
            {
                SpanTime = SpanTime.Add(DishTimer.Interval);
                Duration.DataContext = SpanTime;
            }

     private async void StopBtn_Click(object sender, RoutedEventArgs e)
            {
                //write the recorded stream in memory location

                await CaptureMedia.StopRecordAsync();
                UpdateRecordingControls(RecordingMode.Stopped);
                DishTimer.Stop();
               // DishTimer = null;
                PRing.IsActive = false;

            }

    I want to reset the DishTimer in StopBtn_Click event. But not able to do so.

    Please help me how to do it.

    Thanks,

    Tuesday, February 18, 2014 12:27 PM

Answers

  • Do you mean to reset the time?  If so, just set your SpanTime = TimeSpan.FromSeconds(0);

    Just a note; in TimerOnTick, you should set the SpanTime to the elapsed time since the user hit start, rather than adding 100ms per tick.  If the timer drifts (and it will, the time you display will be 100ms * the tick count, while the actual time elapsed since start would be different.

    For example, add a DateTime startTime variable in your class elsewhere.  In InitTimer, do this:

    startTime = DateTime.Now;

    In your TimerOnTick, display DateTime.Now - startTime.


    Darin R.

    Tuesday, February 18, 2014 2:39 PM