CSE445_M4L5 Events and Delegates
Events and Delegates
Introduction to event-driven programming and its significance in software development.
Overview of the framework used by the Ira A. Fulton Schools of ASU Engineering at Arizona State University.
Lecture Outline
| Topics Covered| Event-driven Paradigm| Delegates and Events| General Event-driven Programming
Control Flow-Based Programming (Polling)
Polling: A method to control the program flow based on certain conditions.
Main Operations:
Temperature measurement.
Breaking news updates.
Exchange rate methods and services.
CPU Utilization: Control flow is active when the processor is available.
Event-Drive Programming
Application of Event-Driven Models:
Controlling motors based on events.
Receiving and processing information from a base station.
Using sensors (e.g., sonar sensor, temperature sensor).
Process Details:
Image processing – involves decompression and compression tasks.
Sending processed information back to the base station.
Implementations of event boards, alert boards, sensor notifications, and handling multiple threads for parallel activities.
Need for Event-Driven Programming
Comparison of polling for different sensors:
Sonar sensor can be polled once per second with alerts for distances shorter than 5 feet.
Touch sensor values must be polled every millisecond to avoid missing inputs.
Importance of writing efficient event-driven programs to manage frequent events without overwhelming the system.
Understanding Events and Event Handling
Design Components:
Events are generated and handled by different classes.
Delegates define the signatures for event handling.
Class A serves as an event service, while Class B and Class C act as event clients subscribing to events.
Callbacks: Allows flexibility in event handling, enabling a one-to-many relationship for subscriptions.
Example: 911 Call Center System
Structure of a simulated 911 call center to handle emergency calls.
Registration of phone numbers and processing of emergencies effectively using event-driven concepts.
Advanced Events and Event Handling Design
Utilizing delegates and subscription lists for effective event handling.
Detailing event generation and orchestration for sensor inputs and actions.
Discussion of built-in support for events in programming frameworks.
C# Delegate for Events and Event Handling
Definition of a Delegate:
A design pattern to encapsulate methods with specific signatures.
Functions as a type-safe function pointer similar to C++.
Enables dynamic method invocation and subscription to callback methods through delegates.
Examples of Delegate Use:
Delegate declarations and instantiations to invoke methods.
Program example illustrating delegate usage with methods Pi_Plus and E_Plus.
Defining Events Using Delegates
Requirements for defining an event:
Creation of a container for subscriptions using delegates.
Implementation of an event emitter function to notify subscribers of events.
Example structures demonstrating the event definition process, including event declarations in C#.
Implementation Example: Event-Driven Programming in C#
Event Handlers:
Definitions for various sensor events (Touch and Motion sensors).
Implementation details including adding and removing event handlers, notifying subscribers with emitted events.
Main Class Execution:
Demonstrates creation of an event class and interactions with event handlers.
Concept Application: Shows practical implementation of event-driven programming as applied to sensor inputs.
Closing Remarks
Recap of key concepts covered in the sections and their implications for programming.
Introduction to upcoming lectures that will expand on these concepts with further detailed examples.
Coordination events in C# are used for
defining the order of execution among the threads.