C# Event Processing
C# Event Processing
C# Event Processing
Model
Agenda
Introduction
C# Event Processing Macro View
Required Components
Role of Each Component
How To Create Each Type Of Component
Introduction
C# is a modern programming language
responsibilities
Consider the following diagram
Object A
Object B
(Event Publisher)
(Event Subscriber)
Subscriber List
(Object B)
Object A maintains a
list of subscribers for
each publishable
event
Button
MainApp
(Event Publisher)
(Event Subscriber)
Subscriber List
(MainApp.onButtonClick)
Button maintains a
list of subscribers of
its
Click event
onButtonClick
processing model
Required Components
To implement custom event processing in
Events
Event Notification Methods
Required Components
You will also need to know how to pass
Example
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.
subscriber list.
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
keyword
Events must be a delegate type
source files:
Delegate.cs
Publisher.cs
Subscriber.cs
MinuteEventArgs.cs
MainApp.cs
using System;
namespace CustomEventExample {
public delegate void ElapsedMinuteEventHandler(Object sender,
MinuteEventArgs e);
} // end CustomEventExample namespace
using System;
namespace CustomEventExample {
public class MinuteEventArgs : EventArgs {
private DateTime date_time;
public MinuteEventArgs(DateTime date_time){
this.date_time = date_time;
}
public int Minute {
get { return date_time.Minute; }
}
}
}
using System;
namespace CustomEventExample {
public class Publisher {
public event ElapsedMinuteEventHandler MinuteTick;
public Publisher(){
Console.WriteLine("Publisher Created");
}
public void countMinutes(){
int current_minute = DateTime.Now.Minute;
while(true){
if(current_minute != DateTime.Now.Minute){
Console.WriteLine("Publisher: {0}", DateTime.Now.Minute);
onMinuteTick(new MinuteEventArgs(DateTime.Now));
current_minute = DateTime.Now.Minute;
}//end if
} // end while
} // end countMinutes method
public void onMinuteTick(MinuteEventArgs e){
if(MinuteTick != null){
MinuteTick(this, e);
}
}// end onMinuteTick method
} // end Publisher class definition
} // end CustomEventExample namespace
using System;
namespace CustomEventExample {
public class Subscriber {
private Publisher publisher;
public Subscriber(Publisher publisher){
this.publisher = publisher;
subscribeToPublisher();
Console.WriteLine("Subscriber Created");
}
public void subscribeToPublisher(){
publisher.MinuteTick += new ElapsedMinuteEventHandler(minuteTickHandler);
}
public void minuteTickHandler(Object sender, MinuteEventArgs e){
Console.WriteLine("Subscriber Handler Method: {0}", e.Minute);
}
} // end Subscriber class definition
} // end CustomEventExample namespace
using System;
namespace CustomEventExample {
public class MainApp {
public static void Main(){
Console.WriteLine("Custom Events are Cool!");
Publisher p = new Publisher();
Subscriber s = new Subscriber(p);
p.countMinutes();
} // end main
} //end MainApp class definition
} // end CustomEventExample namespace