8/18/2019 C# Event Processing ppt
1/25
C# Event Processing
Model
Solving The Mystery
8/18/2019 C# Event Processing ppt
2/25
8/18/2019 C# Event Processing ppt
3/25
Introduction
C# is a modern programming languagesupported y an e$tensive set o %PI structuresand classes& i&e&' The &(et )ramewor*
C# supports event+driven programmingnormally associated with Microsot ,indowsapplications&
"ne normally thin*s o events as einggenerated y -.I components /ut any o0ect can generate an 1event2 i it3s
programmed to do so/
8/18/2019 C# Event Processing ppt
4/25
C# Event Processing Macro View
-enerally spea*ing' two logicalcomponents are required to implementthe event processing model4 56 %n event producer 7or pulisher6
86 %n event consumer 7or suscrier6
Each logical components has assigned
responsiilities Consider the ollowing diagram
8/18/2019 C# Event Processing ppt
5/25
"0ect 97Event Suscrier6
"0ect %7Event Pulisher6
Suscrier :ist7"0ect 96
Event !andler Code
"0ect 9 suscries to event7or events6 generated y "0ect %&
"0ect % maintains alist o suscriers or
each pulishaleevent
,hen an Event occursnoti;cation is sent to allthe suscriers on thelist or that particularevent/
"0ect 9 processes theevent noti;cation in itsevent handler code
C# Event Processing Macro View
8/18/2019 C# Event Processing ppt
6/25
C# Event Processing Macro View
:et3s map "0ect % and "0ect 9 to some amiliar o0ect types/
Main%pp7Event Suscrier6
9utton7Event Pulisher6
Suscrier :ist7Main%pp&on9uttonClic*6
on9uttonClic*
Main%pp suscries to 9utton3sClic* event
9utton maintains alist o suscriers o
itsClic* event
,hen a Clic* eventoccurs noti;cation is sentto all the suscriers onthe list/
Main%pp processes theClic* noti;cation in itson9uttonClic* event
handler code
8/18/2019 C# Event Processing ppt
7/25
C# Event Processing Macro View
These two diagrams hide a lot o details !ow is the suscrier list maintained<
!ow is the event generated<
!ow is noti;cation sent to eachsuscrier<
,hat is an event = really<
!ow can you add custom eventprocessing to your programs<
This presentation attempts to answerthese questions in a clear manner/
8/18/2019 C# Event Processing ppt
8/25
C# Event Processing Macro View
The &(et %PI contains lots o classes thatgenerate di>erent types o events/ Most are -.I related
Can you thin* o a ew< It also contains lots o ?elegates
Can you name at least one<
:et3s ta*e a closer loo* at the C# eventprocessing model
8/18/2019 C# Event Processing ppt
9/25
Required Components
To implement custom event processing inyour programs you need to understandhow to create the ollowing componenttypes4 ?elegates Event -enerating "0ects 7pulishers6
Events Event (oti;cation Methods
Event !andling "0ects 7suscriers6 Event !andler Methods
8/18/2019 C# Event Processing ppt
10/25
Required Components
@ou will also need to *now how to passinormation related to the eventetween the event generating o0ect
and the suscrier o0ect The Event%rgs class can e used as+is or
suclassed The Event%rgs class captures generic
inormation aout an o0ect and the eventthat occured
8/18/2019 C# Event Processing ppt
11/25
Role of Each Component- elegate -
?elegate ?elegate types represent reerences to
methods with a particular parameter list
and return type E$ample
Event!andler7"0ect sender' Event%rgs e6
Represents a method that has two parameters'the ;rst one eing o type "0ect and the second
eing o type Event%rgs& Its return type is void& %ny method' so long as its signature matches
that e$pected y the delegate' can e handledy the delegate&
8/18/2019 C# Event Processing ppt
12/25
Role of Each Component- elegate -
9ut 0ust what is a delegate< % delegate is a reerence type o0ect&
% delegate e$tends either the
System&?elegate or Multicast?elegateclass ?epends on whether one 7?elegate6 or more
7Multicast?elegate6 suscriers are involved
@ou do not e$tend ?elegate orMulticast?elegate The C# compiler does it or you
8/18/2019 C# Event Processing ppt
13/25
Role of Each Component- elegate -
The delegate o0ect contains thesuscrier list& It is actually implemented as a lin*ed list
where each node o the list contains apointer to a suscrier3s event handlermethod
?elegates are types = li*e classes E$cept = you declare them with the
delegate *eyword and speciy the typeso methods they can reerence
8/18/2019 C# Event Processing ppt
14/25
Role of Each Component- Pu!lisher -
% pulisher is any class that can ;re an eventand send event noti;cations to interestedsuscriers
% pulisher class contains the ollowing critical
elements4 %n event ;eld
This is what suscriers suscrie to/ %n event noti;cation method
This activates the suscrier noti;cationprocess when the event occurs
%nd some means o generating the event orrecogniAing the event in question has occurred This usually happens in a method as well
8/18/2019 C# Event Processing ppt
15/25
Role of Each Component- Event -
%n event is a ;eld in a class
Events are declared with the event*eyword
Events must e a delegate type ?elegates' rememer' are o0ects that
contain a list o pointers to suscriermethods that delegate can process
%n event ;eld will e null until the ;rstsuscrier suscries to that event @ou3ll see what I mean y this in a moment
8/18/2019 C# Event Processing ppt
16/25
Role of Each Component- Event "otification Method -
In addition to an event ;eld a pulisherwill have a method whose 0o it is tostart the suscrier noti;cation process
when the event in question occurs %n event noti;cation method is 0ust a
normal method
It usually has a parameter o Event%rgs
or a user+de;ned sutype o Event%rgs& 9ut it can have any numer and type o
parameters you require
8/18/2019 C# Event Processing ppt
17/25
Role of Each Component- u!scri!er -
% suscrier is a class that registers itsinterest in a pulisher3s events
% suscrier class contains one or more
event handler methods
8/18/2019 C# Event Processing ppt
18/25
Role of Each Component- Event $andler Method -
%n event handler methods is anordinary method that is registered witha pulisher3s event&
The event handler method3s signaturemust match the signature required ythe pulisher3s event delegate&
8/18/2019 C# Event Processing ppt
19/25
An Custom Event E%ample
- Elapsed Minute &imer -
This e$ample includes ;ve separatesource ;les4 ?elegate&cs
Pulisher&cs Suscrier&cs
MinuteEvent%rgs&cs
Main%pp&cs
8/18/2019 C# Event Processing ppt
20/25
using SystemB
namespace CustomEventE$ample
pulic delegate void ElapsedMinuteEvent!andler7"0ect sender'MinuteEvent%rgs e6B
D end CustomEventE$ample namespace
8/18/2019 C# Event Processing ppt
21/25
using SystemB
namespace CustomEventE$ample
pulic class MinuteEvent%rgs 4 Event%rgs
private ?ateTime dateFtimeB
pulic MinuteEvent%rgs7?ateTime dateFtime6 this&dateFtime G dateFtimeB D
pulic int Minute get return dateFtime&MinuteB D
D DD
8/18/2019 C# Event Processing ppt
22/25
using SystemB
namespace CustomEventE$ample
pulic class Pulisher
pulic event ElapsedMinuteEvent!andler MinuteTic*B
pulic Pulisher76 Console&,rite:ine7HPulisher CreatedH6B D
pulic void countMinutes76 int currentFminute G ?ateTime&(ow&MinuteB
while7true6 i7currentFminute G ?ateTime&(ow&Minute6Console&,rite:ine7HPulisher4 JDH' ?ateTime&(ow&Minute6B
onMinuteTic*7new MinuteEvent%rgs7?ateTime&(ow66B currentFminute G ?ateTime&(ow&MinuteB Dend i
D end while D end countMinutes method
pulic void onMinuteTic*7MinuteEvent%rgs e6 i7MinuteTic* G null6 MinuteTic*7this' e6B D D end onMinuteTic* method D end Pulisher class de;nitionD end CustomEventE$ample namespace
8/18/2019 C# Event Processing ppt
23/25
using SystemB
namespace CustomEventE$ample
pulic class Suscrier
private Pulisher pulisherB
pulic Suscrier7Pulisher pulisher6 this&pulisher G pulisherB suscrieToPulisher76B Console&,rite:ine7HSuscrier CreatedH6B D
pulic void suscrieToPulisher76 pulisher&MinuteTic* KG new ElapsedMinuteEvent!andler7minuteTic*!andler6B D
pulic void minuteTic*!andler7"0ect sender' MinuteEvent%rgs e6
Console&,rite:ine7HSuscrier !andler Method4 JDH' e&Minute6B
D D end Suscrier class de;nitionD end CustomEventE$ample namespace
8/18/2019 C# Event Processing ppt
24/25
using SystemB
namespace CustomEventE$ample
pulic class Main%pp pulic static void Main76 Console&,rite:ine7HCustom Events are CoolH6B
Pulisher p G new Pulisher76B Suscrier s G new Suscrier7p6B p&countMinutes76B
D end main D end Main%pp class de;nition
D end CustomEventE$ample namespace
8/18/2019 C# Event Processing ppt
25/25