By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

event vs. delegate vs. threads

P: n/a
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.

Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Ondrej Sevecek" <on***@sevecek.com.thisisanuntispamsuffix> wrote in message
news:eu****************@TK2MSFTNGP09.phx.gbl... 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.


Nov 15 '05 #2

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ondrej Sevecek" <on***@sevecek.com.thisisanuntispamsuffix> wrote in message
news:eu****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 15 '05 #3

P: n/a
"Ondrej Sevecek" <on***@sevecek.com.thisisanuntispamsuffix> wrote in message
news:eu****************@TK2MSFTNGP09.phx.gbl...
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!
************************************************** ********************
Nov 15 '05 #4

P: n/a
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

"Ondrej Sevecek" <on***@sevecek.com.thisisanuntispamsuffix> wrote in message
news:eu****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 15 '05 #5

P: n/a
Properties are methods, not fields.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uM**************@TK2MSFTNGP12.phx.gbl...
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

"Ondrej Sevecek" <on***@sevecek.com.thisisanuntispamsuffix> wrote in message news:eu****************@TK2MSFTNGP09.phx.gbl...
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.


Nov 15 '05 #6

P: n/a
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

"Alvin Bruney" <alvin.bruney@.telia..com.> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Properties are methods, not fields.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uM**************@TK2MSFTNGP12.phx.gbl...
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

"Ondrej Sevecek" <on***@sevecek.com.thisisanuntispamsuffix> wrote in

message
news:eu****************@TK2MSFTNGP09.phx.gbl...
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.



Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.