C# Events and Delegates - When do they fire?

G

Guest

So, here is a wierd question that we have been discussing for a bit now.

Does an event fire even though nothing is subscribed to listen to the event?
For instance, does the Click event of a button fire even though nothing is
subscribed to listen to the event.

The answer is NO in a traditional Publisher/Subscriber design pattern (at
least I think it is).

I have stated that if nothing is subscribed, then the event never gets
raised. I would stand by that statement until another developer turned on
Spy++ and it looks like some Windows files are responding to events on a form
ie. Click, MouseOver, etc...). My response to that is Windows must subscribe
to the .NET event so it knows how to do things, like draw a depressed button
when the button gets clicked.

If this is not the case, then I would ask "When creating a custom event, why
does one need to test the event for null before raising the event?" I know
the reason - cuz the delegate is not created. But do standard events (ie.
Click, MouseOver, etc...) not follow the same rule that custom events do?

I know this sounds like a strange discussion and it is like "Does a falling
tree make a sound if no one is around?".

Just curious is anyone can shed some light. I cannot find anything on the
internet that gets into a discussion about this (probably cuz no one cares).

Thanks in advance...
 
N

Nicholas Paldino [.NET/C# MVP]

Ty,

What your colleague is seeing is different from what .NET is exposing.

When you use Spy++, what you are seeing is the windows message being
posted to the button, and the button reacting as a result. Now, when the
messages (or sequence of messages) indicates that an event should fire
(which is very different from a windows message, which is used as an event
notification in that realm), then the instance will attempt to fire the
event, should there be subscribers.

The events that you see (Click, DoubleClick, etc, etc) are the same as
other events. They are fired by code as the result of something happening.
It just so happens that the things that fire the click event on a button is
triggered by a windows message.

So, to answer your question, the event does not fire (in a real sense,
conceptually, it did, since the check for a listener was made) if there are
no subscribers to it.

Hope this helps.
 
G

Guest

Now that makes sense.

So what Spy++ is displaying is really the windows message that checks for
any subscribers that have subscribed to the event. If there are subscribers,
then the event gets raised. So what we are seeing in Spy++ is NOT the event
itself, but simply a windows message to determine whether the event gets
raised or not.

I took it from more of a GDI perspective since I never write code that
depresses a button, for instance.

Hopefully I understood this correctly.

Nicholas Paldino said:
Ty,

What your colleague is seeing is different from what .NET is exposing.

When you use Spy++, what you are seeing is the windows message being
posted to the button, and the button reacting as a result. Now, when the
messages (or sequence of messages) indicates that an event should fire
(which is very different from a windows message, which is used as an event
notification in that realm), then the instance will attempt to fire the
event, should there be subscribers.

The events that you see (Click, DoubleClick, etc, etc) are the same as
other events. They are fired by code as the result of something happening.
It just so happens that the things that fire the click event on a button is
triggered by a windows message.

So, to answer your question, the event does not fire (in a real sense,
conceptually, it did, since the check for a listener was made) if there are
no subscribers to it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ty Salistean said:
So, here is a wierd question that we have been discussing for a bit now.

Does an event fire even though nothing is subscribed to listen to the
event?
For instance, does the Click event of a button fire even though nothing is
subscribed to listen to the event.

The answer is NO in a traditional Publisher/Subscriber design pattern (at
least I think it is).

I have stated that if nothing is subscribed, then the event never gets
raised. I would stand by that statement until another developer turned on
Spy++ and it looks like some Windows files are responding to events on a
form
ie. Click, MouseOver, etc...). My response to that is Windows must
subscribe
to the .NET event so it knows how to do things, like draw a depressed
button
when the button gets clicked.

If this is not the case, then I would ask "When creating a custom event,
why
does one need to test the event for null before raising the event?" I
know
the reason - cuz the delegate is not created. But do standard events (ie.
Click, MouseOver, etc...) not follow the same rule that custom events do?

I know this sounds like a strange discussion and it is like "Does a
falling
tree make a sound if no one is around?".

Just curious is anyone can shed some light. I cannot find anything on the
internet that gets into a discussion about this (probably cuz no one
cares).

Thanks in advance...
 
N

Nicholas Paldino [.NET/C# MVP]

Ty,

No, Spy++ shows you the windows messages that are being sent to windows.

In .NET, the control receives notification when a windows message comes
in for it. Based on the message, it then calls a routine that will fire the
event.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ty Salistean said:
Now that makes sense.

So what Spy++ is displaying is really the windows message that checks for
any subscribers that have subscribed to the event. If there are
subscribers,
then the event gets raised. So what we are seeing in Spy++ is NOT the
event
itself, but simply a windows message to determine whether the event gets
raised or not.

I took it from more of a GDI perspective since I never write code that
depresses a button, for instance.

Hopefully I understood this correctly.

Nicholas Paldino said:
Ty,

What your colleague is seeing is different from what .NET is
exposing.

When you use Spy++, what you are seeing is the windows message being
posted to the button, and the button reacting as a result. Now, when the
messages (or sequence of messages) indicates that an event should fire
(which is very different from a windows message, which is used as an
event
notification in that realm), then the instance will attempt to fire the
event, should there be subscribers.

The events that you see (Click, DoubleClick, etc, etc) are the same
as
other events. They are fired by code as the result of something
happening.
It just so happens that the things that fire the click event on a button
is
triggered by a windows message.

So, to answer your question, the event does not fire (in a real
sense,
conceptually, it did, since the check for a listener was made) if there
are
no subscribers to it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ty Salistean said:
So, here is a wierd question that we have been discussing for a bit
now.

Does an event fire even though nothing is subscribed to listen to the
event?
For instance, does the Click event of a button fire even though nothing
is
subscribed to listen to the event.

The answer is NO in a traditional Publisher/Subscriber design pattern
(at
least I think it is).

I have stated that if nothing is subscribed, then the event never gets
raised. I would stand by that statement until another developer turned
on
Spy++ and it looks like some Windows files are responding to events on
a
form
ie. Click, MouseOver, etc...). My response to that is Windows must
subscribe
to the .NET event so it knows how to do things, like draw a depressed
button
when the button gets clicked.

If this is not the case, then I would ask "When creating a custom
event,
why
does one need to test the event for null before raising the event?" I
know
the reason - cuz the delegate is not created. But do standard events
(ie.
Click, MouseOver, etc...) not follow the same rule that custom events
do?

I know this sounds like a strange discussion and it is like "Does a
falling
tree make a sound if no one is around?".

Just curious is anyone can shed some light. I cannot find anything on
the
internet that gets into a discussion about this (probably cuz no one
cares).

Thanks in advance...
 
K

Kevin Spencer

An event is a delegate method. That is, it is a method which is assigned to
an event variable. The event variable is null until a delegate is assigned
to it. Therefore, unless a delegate is assigned to an event, it will not
fire, as there is nothing to fire. If a delegates is assigned to the event,
it will fire.

Consider the following:

//Event class:
public class MyEventArgs : EventArgs
{
}

//Event Delegate definition:
public delegate void MyEventHandler(object sender, EventArgs e);

//Using the class:
// Creating a variable to hold the delegate.
// Indicates that this is an event delegate of type "MyEventHandler"
public event MyEventHandler HandleEvent;

// Raises the event.
protected override void OnHandleEvent(MyEventArgs e)
{
// It is necessary to check for null, as you are invoking a method
(indirectly)
if (MyEventHandler != null) MyEventHandler(this, e);
}


So, to consume the event, a client needs to assign a delegate Event Handler:
// This is a method that matches the delegate
public void ThisEventHandler(object sender, MyEventArgs e)
{
// handle the event.
}

// The Event Handler method is assigned to the delegate
MyClass.HandleEvent += new MyEventHandler(ThisEventHandler);


--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top