Raising a set of events

  • Thread starter Krzysztof Malinowski
  • Start date
K

Krzysztof Malinowski

Hi,

We can raise an event by calling some method the same
like a delegate. Actually one event can reference a few
different method each of these meets delegate declaration.

Like below:
(...)
clock.Alarm += new AlarmEventHandler(w.AlarmRang);
(...)
where clock.Alarm is an event.

My question is woh can I only in one step call all of
these event handlers, like below:
(...)
protected virtual void OnAlarm(AlarmEventArgs e)
{
if (Alarm != null)
{
// Invokes the delegates.
Alarm(this, e);
}
}
(...)
By calling Alarm(this, e), we actually calling all
handlers.

For me it's a little strange, I think in this way we can
call only last referenced handler. So, I'm used to think
in C++ (pointers) way.

Regards
Krzysztof
 
G

Greg Bacchus

Think of a delegate as a collection of pointers that you can both add to
(using +=) or remove from (using -=). When you call it, it just goes through
all of the pointers in its collection and calls them with the parameters
supplied

Greg
 
C

Chris Hornberger

And IF you want to manage it yourself, you could add a collection
(probably a hashtable) to your explictly designed delegate class and
have that class hold references to all the registered event consumers
in that hashtable. Then when an event fires, you spin thru' your
hashtable and fire the events on X-number of clients. This works best
when you have your clients implement a known interface.
 

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