C Event Processing Model Solving The Mystery Agenda

  • Slides: 25
Download presentation
C# Event Processing Model Solving The Mystery

C# Event Processing Model Solving The Mystery

Agenda n Introduction n C# Event Processing Macro View n Required Components n Role

Agenda n Introduction n C# Event Processing Macro View n Required Components n Role of Each Component n How To Create Each Type Of Component

Introduction n C# is a modern programming language supported by an extensive set of

Introduction n C# is a modern programming language supported by an extensive set of API structures and classes. n i. e. , The. Net Framework n C# supports event-driven programming normally associated with Microsoft Windows applications. n One normally thinks of events as being generated by GUI components n …but any object can generate an “event” if it’s programmed to do so…

C# Event Processing Macro View n Generally speaking, two logical components are required to

C# Event Processing Macro View n Generally speaking, two logical components are required to implement the event processing model: 1) An event producer (or publisher) n 2) An event consumer (or subscriber) n n Each logical components has assigned responsibilities n Consider the following diagram

C# Event Processing Macro View When an Event occurs notification is sent to all

C# Event Processing Macro View When an Event occurs notification is sent to all the subscribers on the list for that particular event… Object B processes the event notification in its event handler code Object A Object B (Event Publisher) (Event Subscriber) Subscriber List (Object B) Event Handler Code Object A maintains a list of subscribers for each publishable event Object B subscribes to event (or events) generated by Object A.

C# Event Processing Macro View Let’s map Object A and Object B to some

C# Event Processing Macro View Let’s map Object A and Object B to some familiar object types… When a Click event occurs notification is sent to all the subscribers on the list… Main. App processes the Click notification in its on. Button. Click event handler code Button Main. App (Event Publisher) (Event Subscriber) Subscriber List (Main. App. on. Button. Click) on. Button. Click Button maintains a list of subscribers of its Click event Main. App subscribes to Button’s Click event

C# Event Processing Macro View n These two diagrams hide a lot of details

C# Event Processing Macro View n These two diagrams hide a lot of details n How is the subscriber list maintained? n How is the event generated? n How is notification sent to each subscriber? n What is an event – really? n How can you add custom event processing to your programs? n This presentation attempts to answer these questions in a clear manner…

C# Event Processing Macro View n The. Net API contains lots of classes that

C# Event Processing Macro View n The. Net API contains lots of classes that generate different types of events… n Most are GUI related n Can you think of a few? n It also contains lots of Delegates n Can you name at least one? n Let’s take a closer look at the C# event processing model

Required Components n To implement custom event processing in your programs you need to

Required Components n To implement custom event processing in your programs you need to understand how to create the following component types: Delegates n Event Generating Objects (publishers) n n Events Event Notification Methods Event Handling Objects (subscribers) n Event Handler Methods

Required Components n You will also need to know how to pass information related

Required Components n You will also need to know how to pass information related to the event between the event generating object and the subscriber object n The Event. Args class can be used as-is or subclassed n The Event. Args class captures generic information about an object and the event that occured

Role of Each Component - Delegate n Delegate types represent references to methods with

Role of Each Component - Delegate n Delegate types represent references to methods with a particular parameter list and return type n Example § Event. Handler(Object sender, Event. Args e) § Represents a method that has two parameters, the first one being of type Object and the second being of type Event. Args. Its return type is void. § Any method, so long as its signature matches that expected by the delegate, can be handled by the delegate.

Role of Each Component - Delegate n But just what is a delegate? n

Role of Each Component - Delegate n But just what is a delegate? n A delegate is a reference type object. n A delegate extends either the System. Delegate or Multicast. Delegate class n n Depends on whether one (Delegate) or more (Multicase. Delegate) subscribers are involved You do not extend Delegate or Multicast. Delegate n The C# compiler does it for you

Role of Each Component - Delegate n The delegate object contains the subscriber list.

Role of Each Component - Delegate n The delegate object contains the subscriber list. n It is actually implemented as a linked list where each node of the list contains a pointer to a subscriber’s event handler method n Delegates are types – like classes n Except – you declare them with the delegate keyword and specify the types of methods they can reference

Role of Each Component - Publisher n A publisher is any class that can

Role of Each Component - Publisher n A publisher is any class that can fire an event and send event notifications to interested subscribers n A publisher class contains the following critical elements: n n n An event field n This is what subscribers subscribe to… An event notification method n This activates the subscriber notification process when the event occurs And some means of generating the event or recognizing the event in question has occurred n This usually happens in a method as well

Role of Each Component - Event n An event is a field in a

Role of Each Component - Event n An event is a field in a class n Events are declared with the event keyword n Events must be a delegate type n Delegates, remember, are objects that contain a list of pointers to subscriber methods that delegate can process n An event field will be null until the first subscriber subscribes to that event n You’ll see what I mean by this in a moment

Role of Each Component - Event Notification Method n In addition to an event

Role of Each Component - Event Notification Method n In addition to an event field a publisher will have a method whose job it is to start the subscriber notification process when the event in question occurs An event notification method is just a normal method n It usually has a parameter of Event. Args or a userdefined subtype of Event. Args. n n But it can have any number and type of parameters you require

Role of Each Component - Subscriber n A subscriber is a class that registers

Role of Each Component - Subscriber n A subscriber is a class that registers its interest in a publisher’s events n A subscriber class contains one or more event handler methods

Role of Each Component - Event Handler Method n An event handler methods is

Role of Each Component - Event Handler Method n An event handler methods is an ordinary method that is registered with a publisher’s event. n The event handler method’s signature must match the signature required by the publisher’s event delegate.

An Custom Event Example - Elapsed Minute Timer n This example includes five separate

An Custom Event Example - Elapsed Minute Timer n This example includes five separate source files: n Delegate. cs n Publisher. cs n Subscriber. cs n Minute. Event. Args. cs n Main. App. cs

using System; namespace Custom. Event. Example { public delegate void Elapsed. Minute. Event. Handler(Object

using System; namespace Custom. Event. Example { public delegate void Elapsed. Minute. Event. Handler(Object sender, Minute. Event. Args e); } // end Custom. Event. Example namespace

using System; namespace Custom. Event. Example { public class Minute. Event. Args : Event.

using System; namespace Custom. Event. Example { public class Minute. Event. Args : Event. Args { private Date. Time date_time; public Minute. Event. Args(Date. Time date_time){ this. date_time = date_time; } public int Minute { get { return date_time. Minute; } }

using System; namespace Custom. Event. Example { public class Publisher { public event Elapsed.

using System; namespace Custom. Event. Example { public class Publisher { public event Elapsed. Minute. Event. Handler Minute. Tick; public Publisher(){ Console. Write. Line("Publisher Created"); } public void count. Minutes(){ int current_minute = Date. Time. Now. Minute; while(true){ if(current_minute != Date. Time. Now. Minute){ Console. Write. Line("Publisher: {0}", Date. Time. Now. Minute); on. Minute. Tick(new Minute. Event. Args(Date. Time. Now)); current_minute = Date. Time. Now. Minute; }//end if } // end while } // end count. Minutes method public void on. Minute. Tick(Minute. Event. Args e){ if(Minute. Tick != null){ Minute. Tick(this, e); } }// end on. Minute. Tick method } // end Publisher class definition } // end Custom. Event. Example namespace

using System; namespace Custom. Event. Example { public class Subscriber { private Publisher publisher;

using System; namespace Custom. Event. Example { public class Subscriber { private Publisher publisher; public Subscriber(Publisher publisher){ this. publisher = publisher; subscribe. To. Publisher(); Console. Write. Line("Subscriber Created"); } public void subscribe. To. Publisher(){ publisher. Minute. Tick += new Elapsed. Minute. Event. Handler(minute. Tick. Handler); } public void minute. Tick. Handler(Object sender, Minute. Event. Args e){ Console. Write. Line("Subscriber Handler Method: {0}", e. Minute); } } // end Subscriber class definition } // end Custom. Event. Example namespace

using System; namespace Custom. Event. Example { public class Main. App { public static

using System; namespace Custom. Event. Example { public class Main. App { public static void Main(){ Console. Write. Line("Custom Events are Cool!"); Publisher p = new Publisher(); Subscriber s = new Subscriber(p); p. count. Minutes(); } // end main } //end Main. App class definition } // end Custom. Event. Example namespace