Skip to main content

Microsoft Silverlight

Answered Question Any reason to use DispatcherTimer over Storyboard?RSS Feed

(3)

frefaln
frefaln

Member

Member

153 points

74 Posts

Any reason to use DispatcherTimer over Storyboard?

I've seen various threads and blogs comparing DispatcherTimer with empty Storyboards. The consensus seems to be that a game loop should be implemented as a Storyboard, since it doesn't execute on the UI thread and utilizes a higher resolution timer. Sounds good to me.

So the question comes to mind, why would I ever use a DispatcherTimer? If I have a dozen or so other timers (not related to the game loop) that aren't critical and can afford to miss their intervals by a bit, am I better off implementing those as DispatcherTimers? Will they use less memory? Do I risk hiccups in the UI if I'm running a bunch of them at once?

Thanks in advance for the enlightenment!

----------------------------------------
Darren Mart
Developer of Steel Saga, which amounted to little more than some free advertising for Silverlight. Glad to help.

sl.ayer
sl.ayer

Participant

Participant

848 points

162 Posts

Answered Question

Re: Any reason to use DispatcherTimer over Storyboard?

In my experience DispetcherTimer is not very accurate. If you need precision of better the 50ms then it is probably not a good idea to use it. DispetcherTimer should be used if you need for your callback to execute in UI thread.

Not sure about memory use, but I can't see it as a major concern with any timer implementation.

Upd: I have to make a correction to the above statement. The accuracy issue with DispetcherTimer is not due to the timer itself, but rather to the fact that its callback is executed in UI thread. UI thread is throttled accordingly with application MaxFrameRate setting and that introduces error of ~1/(MaxFrameRate*2). For example if MaxFrameRate set to 60 then on average callback will be late for 8ms. The smaller your timer interval is the bigger error is going to be. All of the above applies to storyboard's Completed event since it executes in UI thread as well.

Grey Matter
Grey Matter

Member

Member

39 points

108 Posts

Re: Re: Any reason to use DispatcherTimer over Storyboard?

I still have some confusions:

1. How can I be sure that MaxFrameRate value will always be 60 for every computer (including portable computers and so forth)? If 60 fps isn't supported on any machine with Silverlight then my game-loop will make unexpected speed and it won't be good at all.

2. Is there any way to get more frequent (than 60/sec), stable, and platform-independent tick ?

I tried to increase MaxFrameRate value but I didn't reached more than 450/sec and again I'm not sure that on another computer it won't give another result.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities