A new home

I’ve made the move to WordPress!

You should be automatically redirected in 5 seconds. Please visit World Wide Code on Wordpress

Hope to see you there!
Showing posts with label Dispatcher Timer. Show all posts
Showing posts with label Dispatcher Timer. Show all posts

Tuesday, February 1, 2011

DispatcherTimer vs Storyboard


Many of you must have heard and used the Dispatcher Timer class in Silverlight for all sorts of things. One common usage is for animation purpose, which is an incorrect practice.
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. The Dispatcher Timer is also influenced by the UI Thread, so if any heavy rendering is to be done can cause you animation to be rigid and many a times slow.
Silverlight also has a class called Storyboard which can solve these issues and is a better solution than the Dispatcher Timer. The Storyboard has simple properties like TargetProperty, TartgetName which specify which property of which element you would like to animate. The Storyboard supports several animations namely, Color Animation, Double Animation, Point Animation andObject Animation Using Key Frames
Using a Storyboard in XAML


<StackPanel>
  <StackPanel.Resources>
    <!-- Animates the rectangle's opacity. -->
    <Storyboard x:Name="myStoryboard">
      <DoubleAnimation
        Storyboard.TargetName="MyAnimatedRectangle"
        Storyboard.TargetProperty="Opacity"
        From="1.0" To="0.0" Duration="0:0:1"
        AutoReverse="True" RepeatBehavior="Forever" />
    </Storyboard>
  </StackPanel.Resources>

  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100" Height="100" Fill="Blue" />
</StackPanel>
Heres a link to create an Animation in Procedural Code if you need.


In Conclusion
I would suggest avoid using the Dispatcher Timer whenever possible. If you have any queries, you can get back to me.


Related Links