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.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

My Internet Safety Guide I have used scratch to program a guide to internet safety using QR codes.
Using Thread for Animation. Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs.
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Object-Oriented Programming with Java The Java Event Model Lecture 5.
Programming Based on Events
 2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Methods Outline Note: Inconsistent with textbook subsection numbering […] 6.14Recursion 6.15 Example.
Static vs. Dynamic Relationships CIS 480 System Analysis and Design.
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell” and “Programming C#”
Event-based Programming Roger Crawfis. Window-based programming Most modern desktop systems are window-based. Non-window based environment Window based.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 19 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Io package as Java’s basic I/O system continue’d.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Welcome to CIS 083 ! Events CIS 068.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Week 3, Day 1: Processes & Threads Return Quiz Processes Threads Lab: Quiz Lab 3: Strategy & Factory Patterns! SE-2811 Slide design: Dr. Mark L. Hornick.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 1 1 Outline Introduction Windows Forms Event-Handling Model - Basic Event Handling.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”
Introduction to Windows Programming
Event Handling Mohanraj S AP / IT Angel College of Engg & Tech.,
C# GUI - Basics. Objectives.NET supports two types: WinForms, traditional, desktop GUI apps. WebForms – newer, for Web apps. Visual Studio.NET supports.
Controls. Adding Controls to Form -You can pick controls from the toolbox. -To add the controls from Toolbox to the Form You have be in design view. -To.
Concurrent Programming and Threads Threads Blocking a User Interface.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
User Interface Programming in C#: Basics and Events Chris North CS 3724: HCI.
Lesson 28: More on the GUI button, frame and actions.
1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – code –Usage Events Asynchronious delegates.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
 ASP.NET provides an event based programming model that simplifies web programming  All GUI applications are incomplete without enabling actions  These.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
.Net training In gandhinagar 16/10/2016.
Event Loops and GUI Intro2CS – weeks
Chapter 7 - Methods Outline Note: Inconsistent with textbook subsection numbering […] 7.13 Recursion […]
Event loops 16-Jun-18.
Singleton Pattern Command Pattern
Ellen Walker Hiram College
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Event Driven Programming
Event Driven Programming
The Execution Of Sleep()
C# Event Processing Model
Event loops.
Delegates & Events 1.
Tonga Institute of Higher Education
Event loops 17-Jan-19.
Event loops 17-Jan-19.
6. WinForms 2003 C# GUI - Basics.
Event loops 8-Apr-19.
Chapter 13: Handling Events
Event loops.
Event loops.
Event loops 19-Aug-19.
Multi-Threaded Systems with Queues
Presentation transcript:

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 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.

Event Notification Pattern in C#

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.

Egg Timer User Interface

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.

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

.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.

Design Classes

Egg Timer Sequence

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

// 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)

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

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(); }