In Flash and Silverlight, there are many ways to call a method by a regular time interval. Today I will use a simple sample to illustrate their differences on the following methods:
- Flash - flash.utils.Timer
- Flash - Event.ENTER_FRAME
- Silverlight - System.Windows.Threading.DispatcherTimer
- Silverlight - CompositionTarget.Rendering
The samples are running with 10 FPS (Frame rate per second) and the timer interval is set to be 100 milliseconds.
Vote for this sample
Comparison
Flash implementation: 30 minutes
Silverlight implementation: 30 minutes (Implemented First)
What’s the difference?
- Flash: Timer vs Enter Frame
- Silverlight: Dispatcher Timer vs Composition Target
Source codes
Timer Testing [Flash 9, AS3] (10 KiB, 258 hits)
Timer Testing [Silverlight 2, C#] (33.8 KiB, 312 hits)
Flash
Silverlight
Flash: Timer vs Enter Frame
(The result I described below may be varies in different browser or different OS)
Both of the method generated exactly the same count value. However, you will find that the number of handlers are called less than expected.
It is supposed for each second, the counter will be increased by 10. But the actual case is that the counter is less than the expected value as time goes by.
Silverlight: Dispatcher Timer vs Composition Target
I think Silverlight has more funny outcomes.
Firstly, the increment in Dispatcher Timer is far less than the value in that of Composition Target. (They are expected to be the same.)
Besides that, CompositionTarget.Tick will be stopped if the Application is not shown on the screen. Here is a simple trial, just scroll up your browser until the Application is completely moved out of the screen. Scroll back to the Application a while later. You will find that Dispatcher Timer will has a bigger value.
January 10th, 2009 at 6:46 pm
Incredible diference between size. 253 KB in Flash vesus 34 KB in Silverlight.
January 11th, 2009 at 1:13 am
I am only getting 7 FPS on the flash
January 11th, 2009 at 3:08 am
Some mistake when saving the FLA. The actual file size should be 10KB only.
January 11th, 2009 at 12:12 pm
It appears that if you need a heartbeat in SL, you better use dispatchtimer, or a storyboard timer(if you need better accuracy), instead of the newer Composition Target.
Composition Target is actually better than dispatch in accuracy, but not working if the control isn’t visible is horrible.
January 11th, 2009 at 12:19 pm
Hey Terence,
I liked your Grid Transition Rotator. For the Flash Version, would you consider making it grab images from the server?
That would make it more generic and useful. Thanks.
January 11th, 2009 at 6:49 pm
Fallon Massey,
Thanks for the suggestion. I am considering to launch a new site mainly for image rotators. More features, like the one you mentioned, will be added as well.
January 12th, 2009 at 1:21 am
I rewrote your Flash implementation and for some reason I get much better results. The frame counter is updating perfectly while the timer lags a little behind (which is natural since Flash’s timers are only firing on the frame after the timer expires (and not on the exact millisecond)). I work with Flash daily and we use both timers and frame listeners and we have never seen the lag that you have in your application.
January 12th, 2009 at 3:03 pm
Good idea, keep us informed.
January 13th, 2009 at 5:03 am
[...] is showing the differences between Flash’s Enter frame and Silverlight Composition Target - http://www.shinedraw.com/mathematics/difference-on-timer-enter-frame-dispatcher-timer-and-compositio... Date January 12th, 2009 Filed in [...]
January 13th, 2009 at 5:50 pm
[...] SNow has his latest up … can you believe he’s up to 80?? .. this one is good info on clipping. Difference on Timer, Enter Frame, Dispatcher Timer and Composition Target Terence Tsang has another post up today, in this one the Silverlight is comparing DispatcherTimer [...]
January 20th, 2009 at 4:41 pm
@Vitor Canova Where did you get 253 KB? The swf file is 3kb.
@Daniel Dowell The lag Shine gets is from setting the frame rate of the Flash movie to 10.
@shine “It is supposed for each second, the counter will be increased by 10. But the actual case is that the counter is less than the expected value as time goes by.”
If you want the timer event to be called every second then your timer constructor should say _timer = new Timer(1000); and not _timer = new Timer(1000/10);
1000milliseconds = 1 second
With the whole voting, I think you have more SilverLight developers on this blog then Flash :)
February 13th, 2009 at 6:15 am
CompositionTarget is not a timer! It shouldn’t be used as a timer. It is great that Microsoft shuts down rendering then silverlight control isn’t visible. This way it will use less CPU resources.
SL does better job in timer department then Flash.