1

The timer in my app is doing some change and I want it to stop when the change is enough for me when it executes the stop() method it doesn't stop.

timerpartAnimation = new DispatcherTimer();
timerpartAnimation.Interval = TimeSpan.FromMilliseconds(1);
timerpartAnimation.Tick += timer_Tick_Anime;
timerpartAnimation.Start();

public void timer_Tick_Anime(object sender, object e)
    {
        newL-=3;
        newT = A * newL + B;
        playPart33.Margin = new Thickness(newL, newT, playPart33.Margin.Right, playPart33.Margin.Bottom);
        if (dis(playPart33.Margin, currentIm.Margin))
        {
            timerpartAnimation.Stop();
            currentIm.Visibility = Windows.UI.Xaml.Visibility.Visible;
        }
    }

the weird thing is that this line:

currentIm.Visibility = Windows.UI.Xaml.Visibility.Visible;

does happening so it is deffenetly enter the if scope.

anyone has an idea?

  • What is the value of [`timerpartAnimation.IsEnabled`](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.isenabled.aspx)? Where are you attaching the timer event? You could be [attaching the `tick` event multiple times](http://stackoverflow.com/a/8080969/175679). Have you tried [stopping it using the `Dispatcher.BeginInvoke()`](http://stackoverflow.com/a/7709671/175679). – SliverNinja - MSFT Aug 03 '13 at 14:17
  • 1
    My guess is that with a 1 ms interval you are getting multiple firings of your event. Try experimenting with a 100ms interval to see if it makes a difference. – Mark Hall Aug 03 '13 at 14:20
  • I tryed 100 ms didn't work – user2265259 Aug 03 '13 at 16:44
  • how do I use the Dispatcher.BeginInvoke() ? – user2265259 Aug 03 '13 at 16:45

1 Answers1

0

You can write a ticking function.

   public static void ticking(bool tick)
   {
    if(tick)
    {
    timerpartAnimation.Interval = TimeSpan.FromMilliseconds(1);
    }
    else
    {
    timerpartAnimation.Interval = TimeSpan.FromMilliseconds(0);
    }
   }

so ticking(false); means stop the timer.

Emre Acar
  • 920
  • 9
  • 24