K
Kent
The following is a greatly simplified example of what I would like to
accomplish using the event/delegate features of .NET
I have a class "NumGen" that produces a random set of numbers between 0 and
1000. Each time a number is generated an event called "NewNumber" is
raised. I have another class "NumReceiver" with anywhere between 25 and 50
instances where each instance is interested in only a certain number
produced by NumGen. Some instances of NumReceiver may be interested in the
same number though.
My first thought is that each instance of NumReceiver would implement an
event handler for NewNumber as shown below.
// Event Handler for 1st instance of NumReceiver
..... NumGen.NewNumber += System.EventHandler(this.numReceiver1_NewNumber);
..... numReceiver1_NewNumber(NumGenEventArgs e)
{
if (e.NewNumber == 100)
{
// process number
}
}
// Event Handler for 2nd instance of NumReceiver
..... NumGen.NewNumber += System.EventHandler(this.numReceiver2_NewNumber);
..... numReceiver2_NewNumber(NumGenEventArgs e)
{
if (e.NewNumber == 495)
{
// process number
}
}
{{{ Repeat above for 25 to 50 instances }}}
The problem is that each time a new number is generated by NumGen all
registered receivers of the NewNumber event will be called, even if none of
the instances are interested in the new number, which will greatly impact
performance since the numbers produced by NumGen are coming in pretty fast.
Ideally, I would like the NewNumber event to fire only on those instances of
NumReceiver whose number was generated but I don't know if this is possible.
A less than ideal but acceptable option is to fire the NewNumber event on
all NumReceiver instances when a generated number matches any one instances.
I guess what I am looking for is a way to qualify which receivers of the
NewNumber event are called based on their interest in a specific number. Is
this kind of processing possible using the event/delegate processing
features of .NET? If so, how would you define a delegate or event in the
NumGen class that could handle this scenario.
Thanks for any help you can provide,
Kent
accomplish using the event/delegate features of .NET
I have a class "NumGen" that produces a random set of numbers between 0 and
1000. Each time a number is generated an event called "NewNumber" is
raised. I have another class "NumReceiver" with anywhere between 25 and 50
instances where each instance is interested in only a certain number
produced by NumGen. Some instances of NumReceiver may be interested in the
same number though.
My first thought is that each instance of NumReceiver would implement an
event handler for NewNumber as shown below.
// Event Handler for 1st instance of NumReceiver
..... NumGen.NewNumber += System.EventHandler(this.numReceiver1_NewNumber);
..... numReceiver1_NewNumber(NumGenEventArgs e)
{
if (e.NewNumber == 100)
{
// process number
}
}
// Event Handler for 2nd instance of NumReceiver
..... NumGen.NewNumber += System.EventHandler(this.numReceiver2_NewNumber);
..... numReceiver2_NewNumber(NumGenEventArgs e)
{
if (e.NewNumber == 495)
{
// process number
}
}
{{{ Repeat above for 25 to 50 instances }}}
The problem is that each time a new number is generated by NumGen all
registered receivers of the NewNumber event will be called, even if none of
the instances are interested in the new number, which will greatly impact
performance since the numbers produced by NumGen are coming in pretty fast.
Ideally, I would like the NewNumber event to fire only on those instances of
NumReceiver whose number was generated but I don't know if this is possible.
A less than ideal but acceptable option is to fire the NewNumber event on
all NumReceiver instances when a generated number matches any one instances.
I guess what I am looking for is a way to qualify which receivers of the
NewNumber event are called based on their interest in a specific number. Is
this kind of processing possible using the event/delegate processing
features of .NET? If so, how would you define a delegate or event in the
NumGen class that could handle this scenario.
Thanks for any help you can provide,
Kent