Challenging Event Processing Problem

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Kent,

What about if you create an array of the delegate:

protected DelegateName[] arr = new DelegateName[1000]; // with the number
of possible values

Then you use a public method to hook the delegate to the correct index:

public HookIt( DelegateName newhandler, int index)
{
arr[index ] += newhandler; // you have to check for index in range
}


Then when the new number is generate it's only dispatched to the correct
handler:

arr[ RandonNumber]( parameter_list );



Hope this help,
 
K

Kent

Ignacio,

Thanks for the suggestion. I think what you have described will provide a
lot better performance over using built-in event processing, and it's easy
to implement.

Thanks again,
Kent

Ignacio Machin ( .NET/ C# MVP ) said:
Hi Kent,

What about if you create an array of the delegate:

protected DelegateName[] arr = new DelegateName[1000]; // with the number
of possible values

Then you use a public method to hook the delegate to the correct index:

public HookIt( DelegateName newhandler, int index)
{
arr[index ] += newhandler; // you have to check for index in range
}


Then when the new number is generate it's only dispatched to the correct
handler:

arr[ RandonNumber]( parameter_list );



Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Kent said:
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
 
E

Eric Newton

Great suggestion ignacio: a new perspective on the orginal problem

And it'll be a ton more efficient because only the ones that want a
particular number will be called.


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Kent said:
Ignacio,

Thanks for the suggestion. I think what you have described will provide a
lot better performance over using built-in event processing, and it's easy
to implement.

Thanks again,
Kent

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:[email protected]...
Hi Kent,

What about if you create an array of the delegate:

protected DelegateName[] arr = new DelegateName[1000]; // with the number
of possible values

Then you use a public method to hook the delegate to the correct index:

public HookIt( DelegateName newhandler, int index)
{
arr[index ] += newhandler; // you have to check for index in range
}


Then when the new number is generate it's only dispatched to the correct
handler:

arr[ RandonNumber]( parameter_list );



Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Kent said:
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
 

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