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 and Object 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

Advertisements

3 thoughts on “DispatcherTimer vs Storyboard

  1. Hi Shubhan,

    Thanks for the information. From your explanation, storyboards are useful if you need animations and all where DispatcherTimer doesn’t work well. But I’m still confused as a newbie to SL and WPF, out of two what I can use to trigger an execution of the function after specific interval. Just like we have System.Timers.Timer in windows.

    Thanks for the help.

    1. Hi Rohit,
      Well the dispatcher timer would be ideal if you want to just execute a function after a specific interval. However, if your function is doing a lot of UI related tasks then the actual timer interval applied could be greater than the one you have defined. In that case, you might have to reconsider your implementation, or use a storyboard. I strongly feel that storyboards should be just at the UI level and must not be used to do a certain reoccurring task (which does thing other than modifying the UI). Also, you could use a CallMethodAction or TimerTrigger triggers which are similar to storyboards (defined in the Microsoft.Expression.Interactions.dll), if they suffice your requirements.

      Hope this answers your question.

  2. Thanks Shubhan,

    finally I ended up using System.Threading.Timer class for the operation.

    Thanks for your support.

    Rohit

Share your thoughts

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s