Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Notification Pattern Dr. Neal CIS 480. Event Notification Pattern When a class abc has something happening that class xyz needs to know about you.

Similar presentations


Presentation on theme: "Event Notification Pattern Dr. Neal CIS 480. Event Notification Pattern When a class abc has something happening that class xyz needs to know about you."— Presentation transcript:

1 Event Notification Pattern Dr. Neal CIS 480

2 Event Notification Pattern When a class abc has something happening that class xyz needs to know about you can use the event/notification pattern. This is the pattern you have used between the Windows O/S and your windows or pages. When a mouse click occurs, Windows queues the mouse click message in the system dispatcher, determines where on the desktop it occurred, then determines which window is at the top Z-level at that location, and finally sends the message to that window. This is why you wire-up your event methods with Windows notifications that are passed down through the Form or Page class hierarchy. This is great for Windows O/S level events, but what if we what to send notifications between your own classes. For example, what if a field is changed on abc Form that you have displayed on another xyz Form. Form abc could send an event notification to xyz to change its value. This would require Form abc to know about a method to call in xyz. The xyz method could change the value.

3 Event Notification Pattern in C#

4 Egg Timer Example Say we wish to create an egg timer that allows us to start the timer and be notified when the time is finished. Our design approach: –EggTimer class (a window Form) –Timer class (a class that will run for 1 minute and notify the EggTimer class when the minute is up.

5 Egg Timer User Interface

6 Other Design Criteria To allow the user to (theoretically) work in the EggTimer Form while waiting to be informed that the egg is done, we will put the Timer object on a separate thread of execution. This will allow the EggTimer Form to just wait until notified by an event in Timer class. This means that our Timer class will have to create an event “timerdone” and the EggTimer class will have to register a method “eggdone” with this event. When the Timer class finishes waiting a minute it will call the event “timerdone” that will in turn notify EggTimer by calling its “eggdone” method. The “eggdone” method will just display the message egg done in the TextBox.

7 Using the Event Notification Pattern Timer This is the event NOTIFIER class. EggTimer This is the event SUBSCRIBER class. Event calls method “eggdone” in EggTimer after one minute EggTimer must register “eggdone” method with The “timerdone” event in Timer Timer must create the event “timerdone” and define the signature of the delegate

8 .NET and Complier Generated Classes Event – the compiler generated event class Delegate – an object describing a reference to method with a specified signature Thread -.NET class for creating and running a separate thread of execution ThreadStart –.NET object containing a method reference to be run when a thread starts TimeDate –.NET structure to store the system time so that we can make time comparisons.

9 Design Classes

10 Egg Timer Sequence

11 public class EggTimer : System.Windows.Forms.Form { private System.Windows.Forms.Label lblNotification; private System.Windows.Forms.TextBox txtNotification; private System.Windows.Forms.Button btnStartTimer; private System.ComponentModel.Container components = null; // the timer class is created and run on a separate thread // to keep track of the changes in system time private Timer timer; // the thread object is used to start and run another a // thread of execution for the timer private Thread t; public EggTimer() { InitializeComponent(); } [STAThread] static void Main() { Application.Run(new EggTimer()); } EggTimer Class

12 // when the starttimer button is clicked this method // creates the timer object attaches the event handler // method to it and runs it on a separate thread of // execution private void startTimerClick(object sender, System.EventArgs e) { timer = new Timer(); // wireup the eggDone method to be notified when the timer // has the correct elapsed time timer.timerdone += new Timer.NotifyHandler(eggDone); // create a new thread and start the timer running on it // note the timer.runIt() method will be called for the // second thread to begin execution t = new Thread(new ThreadStart(timer.runIt)); t.Start(); // must ask this thread to share sense we have only a single processor Thread.Sleep(0); } // this is the method called from timer when it fires its associated timerdone // event, it just tells us that the egg timer has finished by displaying text public void eggDone() { txtNotification.Text = "Egg Done"; t.Join(); } EggTimer Class (cont)

13 public class Timer { // create a delegate class attribute of type "void method()" public delegate void NotifyHandler(); // create a new event called timerdone which calls delegate of type NotifyHandler public event NotifyHandler timerdone; // attributes to contain the starting and end DateTime structures private DateTime endtime; private DateTime currenttime; public Timer() { // construct the object and set the ending time to the current // time plus one minute endtime = DateTime.Now; endtime = endtime.AddMinutes((double).2); } Timer Class

14 public void runIt() { // this method is run a separate thread and loops infinitely until // the current system time is greater than the initialized endtime // note: that the DateTime structure has overloaded operators for // comparsion so that > can be used to compare the structures // when one minute has expired the fireEvents method is called and // execution on the thread for timer ends while (true) { currenttime = DateTime.Now; if (currenttime > endtime) { fireEvents(); break; } Thread.Sleep(0); } public void fireEvents() { // a check is made to see the timerdone event contains any NotifyHandlers // if so they are all notified by the call timerdone(), events have the logic // to call all the methods which have registered for notification if (timerdone != null) { timerdone(); }


Download ppt "Event Notification Pattern Dr. Neal CIS 480. Event Notification Pattern When a class abc has something happening that class xyz needs to know about you."

Similar presentations


Ads by Google