Download presentation
Presentation is loading. Please wait.
1
C# Event Processing Model
1
2
C# Event Processing Macro View
Generally speaking, two logical components are required to implement the event processing model: 1) An event producer (or publisher) 2) An event consumer (or subscriber) Each logical component has assigned responsibilities Consider the following diagram
3
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 (Event Publisher) Object B (Event Subscriber) Object B subscribes to event (or events) generated by Object A. Subscriber List (Object B) Event Handler Code Object A maintains a list of subscribers for each publishable event
4
C# Event Processing Macro View
This diagram hides a lot of details How is the subscriber list maintained? How is the event generated? How is the notification sent to each subscriber? What is an event – really? How can you add custom event processing to your applications?
5
Required Components To implement custom event processing in your programs you need to understand how to create the following component types: Delegates Event Generating Objects (publishers) Events Event Notification Methods Event Handling Objects (subscribers) Event Handler Methods
6
Required Components You will also need to know how to pass information related to the event between the event generating object and the subscriber object The EventArgs class can be used as-is or subclassed The EventArgs class captures generic information about an object and the event that occurred
7
Role Of Each Component Delegate Example
Delegate types represent references to methods with a particular parameter list and return type Example void EventHandler(Object sender, EventArgs e) Represents a method that has two parameters, the first one being of type Object and the second being of type EventArgs. Its return type is void Any method, so long as its signature matches that expected by the delegate, can be handled by the delegate
8
Role Of Each Component Delegate A delegate is a reference type object.
A delegate extends either the System.Delegate or MulticastDelegate class Depends on whether one (Delegate) or more (MulticaseDelegate) subscribers are involved You do not extend Delegate or MulticastDelegate The C# compiler does it for you
9
Role Of Each Component Delegate
The delegate object contains the subscriber list It is actually implemented as a linked list where each node of the list contains a pointer to a subscriber’s event handler method Delegates are types – like classes Except – you declare them with the delegate keyword and specify the types of methods they can reference
10
Role Of Each Component Publisher
A publisher is any class that can fire an event and send event notifications to interested subscribers A publisher class contains the following critical elements: An event field This is what subscribers subscribe to… An event notification method 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 This usually happens in a method as well
11
Role Of Each Component Event An event is a field in a class
Events are declared with the event keyword Events must be a delegate type Delegates, remember, are objects that contain a list of pointers to subscriber methods that delegate can process An event field will be null until the first subscriber subscribes to that event
12
Role Of Each Component Event Notification Method
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 It usually has a parameter of EventArgs or a user-defined subtype of EventArgs. But it can have any number and type of parameters you require
13
Role Of Each Component Subscriber
A subscriber is a class that registers its interest in a publisher’s events A subscriber class contains one or more event handler methods
14
Role Of Each Component Event Handler Method
An event handler methods is an ordinary method that is registered with a publisher’s event The event handler method’s signature must match the signature required by the publisher’s event delegate
15
?
16
References
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.