Conditional events?

  • Thread starter Thread starter P. Gorecki
  • Start date Start date
P

P. Gorecki

Hi,

I have a Sender and Reciever classes. I want the sender to send events
to reciever but given a condition specified by reciever.

For example, lets assume that sender is a keyboard and a reciever is a
console. Lets assume that keyboard sends event every time key is
pressed:

Standard way of adding event handlers:

//somewhere in console class initialization
keyboard.EventHandler += new SomeEventDelegate(this.OnEvent);

Now, if key is pressed, console will recieve an event from keyboard
and the key code can be included in SomeEventArgs structure.

The big question is, how can console provide information to the
keyboard on which keys it wants to listen (recieve events), in other
words to have something like this:

//somewhere in console class initialization
keyboard.AddHandler(this.OnSpecificKeyPressed, key);
 
P. Gorecki,

You will have to add custom code to do this on your event producer.
Generally, I think that this is a bad idea, especially if you are providing
filters for individual subscribers. If you want individual subscribers to
filter themselves, then they have all the information that they need in the
event handler to make an informed decision.

If you want to filter the events to all subscribers, I would place a
property or method that will set the condition, and then have your event
firer check to make sure that condition is true when firing.

Hope this helps.
 
You could create a method which takes the delegate instance and the condition and internally wires up the event and stored the filter data. In other words you could have exactly what you have stated, an AddHandler method. You could store which delegates to invoke on which keys via a hashtable.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi,

I have a Sender and Reciever classes. I want the sender to send events
to reciever but given a condition specified by reciever.

For example, lets assume that sender is a keyboard and a reciever is a
console. Lets assume that keyboard sends event every time key is
pressed:

Standard way of adding event handlers:

//somewhere in console class initialization
keyboard.EventHandler += new SomeEventDelegate(this.OnEvent);

Now, if key is pressed, console will recieve an event from keyboard
and the key code can be included in SomeEventArgs structure.

The big question is, how can console provide information to the
keyboard on which keys it wants to listen (recieve events), in other
words to have something like this:

//somewhere in console class initialization
keyboard.AddHandler(this.OnSpecificKeyPressed, key);

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 22/10/2004



[microsoft.public.dotnet.languages.csharp]
 
Back
Top