event vs. delegate vs. threads

  • Thread starter Thread starter Ondrej Sevecek
  • Start date Start date
O

Ondrej Sevecek

Hello,
what is the difference between "event" and the only use of delegate? Why
one should note events with "event" keyword when the functionality seems the
same as with pure delegates?

When I call some delegate, in which thread this delegate gets called -
callers = synchronous, or some other thread = asynchronous? And in what
thread is this done with "event"?

Ondra.
 
Ondra,

With events, the firing of the event is restricted to the class that
defined the event. This is a good thing, because if you just exposed a
public field that was a delegate, then anyone could invoke the delegate, and
you would have undesirable behavior.

When you call an event or a delegate synchronously, the event handlers
execute on whatever thread the delegate was invoked on. When you call it
asynchronously, the call will be executed on a thread other than the thread
that initiated the call.

Hope this helps.
 
Hello Ondrej,
what is the difference between "event" and the only use of delegate? Why
one should note events with "event" keyword when the functionality seems the
same as with pure delegates?

This improves code readability. A notion of event is much more
understandable.
Then, it is more convenient to raise events than to invoke delegates.
When I call some delegate, in which thread this delegate gets called -
callers = synchronous, or some other thread = asynchronous? And in what
thread is this done with "event"?

In both cases, calls are synchronous, unless you use BeginInvoke.
 
Ondrej Sevecek said:
Hello,
what is the difference between "event" and the only use of delegate? Why
one should note events with "event" keyword when the functionality seems the
same as with pure delegates?

Behind the scenes, the functionality is pretty much the same. The delegate
is a bit more flexible, as you can hook multiple handlers in to create
multi-casting "events".
When I call some delegate, in which thread this delegate gets called -
callers = synchronous, or some other thread = asynchronous? And in what
thread is this done with "event"?

Unless you code it otherwise, the default is synch.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Ondrej,
In addition to the others comments:

The way I view it is:

Events are implemented in terms of a Delegate, similar to how a Property is
implemented in terms of a Field.

The Event protects the underlying Delegate, similar to how a Property
protects the underlying Field.

In other words you use Events when you want to encapsulate the Delegate,
similar to how you use a Property when you want to encapsulate a Field.

Hope this helps
Jay
 
Properties are methods, not fields.


Jay B. Harlow said:
Ondrej,
In addition to the others comments:

The way I view it is:

Events are implemented in terms of a Delegate, similar to how a Property is
implemented in terms of a Field.

The Event protects the underlying Delegate, similar to how a Property
protects the underlying Field.

In other words you use Events when you want to encapsulate the Delegate,
similar to how you use a Property when you want to encapsulate a Field.

Hope this helps
Jay
 
Alvin,
Did you read what I stated? Did you understand what I said?

No place did I state a Property IS A Field, I made an analogy relating a
property to a field, however that is relating not stating is a!

I know properties are methods. It happens to be a method that normally
encapsulates a field.

Hope this helps
Jay
 
Back
Top