A stunning effect with a laser orbiting around the mouse! In this example, it demonstrated the difference in dispatching Event between AS3 and C#.
It included both Flash and Silverlight versions with complete source codes.
Comparison
Flash implementation: 45 minutes
Silverlight implementation: 1 hour
What’s the difference?
- Event Dispatch: dispatchEvent(Event.COMPLETE) [AS3] vs EventHandler Completed [C#]
Source codes
Laser Orbit [Flash 9, AS3] (9.4 KiB, 405 hits)
Laser Orbit [Silverlight 2, C#] (42.9 KiB, 465 hits)
Flash
Silverlight
dispatchEvent(Event.COMPLETE) [AS3] vs EventHandler Completed [C#]
In AS3, dispatching an event is easy and you can dispatch an event with any name you like. It enables you to manage the state of the object easily. However, you Class will go messy if you didn’t manage it well. Besides, bubbling an event is very simple, too.
// AS3
// add the event handler
this.addEventListener("ANY_EVENT_NAME", on_complete);
// dispatch the event (using any Event Name you like)
dispatchEvent(new Event("ANY_EVENT_NAME"));
// event handler
function on_complete(e:Event):void{
// do something here
}
// bubbling event, the event will bubble up through the
// display object tree list until it reach the root
dispatchEvent(new Event("ANY_EVENT_NAME", true));
While in C#, the style is different than that in AS3. If you want to define your own event, you have to define the EventHandler variable first. For event bubbling, it is much more complicated than AS3. (I need to study this more before talking about this.)
// C#
// eefine the event
public event EventHandler Completed;
// add the event handler
Completed += new EventHandler(this_Completed);
// dispatch the event
Completed(this, new EventArgs());
// event handler
void this_Completed(object sender, EventArgs e)
{
// do something here
}
September 4th, 2008 at 12:31 pm
in terms of cpu and resource usage, silverlight is clearly the winner (i checked cpu and os load while moving the mouse)….the difference is quite substantial and i doubt it has much to do with a possible inefficient flash implementation of this example
September 4th, 2008 at 2:12 pm
Scott, thanks for the testing. Can you also post some of you testing results?
By the way, in the Flash Version, the “Lazer” is loaded from the FLA rather generated by code. May be the it is one reason for making the differences. But somehow, I do think that Silverilght can really work better than Flash in some aspect. Just like Chrome is faster than other Browers in Javascript processing.
September 4th, 2008 at 3:24 pm
Another great posting.
September 4th, 2008 at 8:55 pm
With C# there are few other things you can do with the code samples you provided.
When you’re calling (dispatching) your event, you should do EventArgs.Empty instead of instantiating a new instance of EventArgs.
With .NET 3.5 you don’t have to instantiate the EventHandler when you add to the event delegate. Instead, you can just reference the event handler method. This is just syntactic sugar, but nice. Example:
Completed += this_Completed;
Also, instead of creating a method for the event handler, you can use a lamba expression with an anonymous method. This can be great for events that are very simple, or if you want to use closures. Example:
string closure = “Example”;
Completed += (sender, e) =>
{
string localScope = closure.ToLower();
};
There’s lots of other things you can do too, like defining your own event handler to pass custom data to the event or instead of adding your event handler in code, you can do that in XAML.
Thanks for the demo and code comparison.
September 5th, 2008 at 10:58 am
Matt Casto, thanks for sharing. I think the event handling in C# is powerful but also take time to master it. I will have more deeper study on that in the coming days.
By the way, your blog is cool. Just wanna ask if visitor can leave message or not?
September 5th, 2008 at 8:22 pm
You should be able to leave a comment.
Currently its not working quite right, so I don’t get emails telling me when people have left a comment. I need to work on that, but lately I’ve been playing with Silverlight in all of my free time. ;-)
September 29th, 2008 at 12:12 pm
@Matt,
The syntactic sugar you mention with lambdas/closures is nice. Except, I do not like it when you have to unwire events, because (as far as I know) you can’t do it. So, its has a syntactic/coolness factor but does limit you when your doing things like control development when in certain states events are unwired.
– Bart
October 21st, 2008 at 11:48 pm
This is a really interesting blog post,I have added your blog to my favourites I really like it,keep up the good work!Im sure I have commented on your blog before.
November 4th, 2008 at 8:00 am
The topic is quite curious, i must say
November 19th, 2008 at 1:39 am
Nice blog I really like your writing style,it is really interesting to read your articles.