



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Week Lecture length Learning outcome Week 1 3 hours LO
● Define event-driven programming and an event. ● Identify an event source, an event handler, a publisher and a subscriber. ● Distinguish sequential execution from event-driven execution. ● Interpret a simple Windows Forms event handler. ● Classify an event by the category it belongs to.
Event Handler
private void btnLogin_Click( object sender, EventArgs e) { lblStatus.Text = "Login button clicked"; } The handler is named btnLogin_Click. It runs when the associated Login button produces a Click event. The statement inside the method changes the text displayed by lblStatus. Publisher and Subscriber A publisher is an object that announces or raises an event. A subscriber is an object or method that has registered to receive that event. The publisher does not need to know every detail of what subscribers will do after receiving the notification. This separation lets different parts of a program respond to the same occurrence. Basic publisher-subscriber model After an online payment succeeds, one part of the system can create a ticket, another can send an email and another can record the sale. All three actions are responses to the same payment event. Event Loop An event loop is the mechanism that keeps an application ready to receive events and dispatch the correct response. In most graphical frameworks, the programmer does not write the full event loop. The framework manages the waiting and dispatching, while the programmer writes the handlers for the events that matter to the application. Event source Event Event loop Handler 1 Handler 2 Who causes it What happened Handler 3 Delivers the notification
The two styles appear inside the same application. An event handler contains several statements that execute sequentially. Event-driven programming does not remove normal program flow; it determines when particular blocks of code run.
.NET provides built-in support for event-driven programming. C# uses delegates and events to connect a notification to one or more methods. Windows Forms provides controls such as Button, TextBox, CheckBox, ComboBox and Timer that expose events. ASP.NET Core handles web requests through routes, controllers, endpoints and middleware, while message systems notify services when new messages arrive.
private void btnWelcome_Click( object sender, EventArgs e) { string studentName = txtName.Text; lblMessage.Text = "Welcome " + studentName; } The method does not run continuously. It runs after the Click event occurs. txtName.Text reads what the user typed. The result is assigned to lblMessage.Text, changing what appears on the form.
Event Subscription
btnWelcome.Click += btnWelcome_Click;
Area Typical events Desktop software Button clicks, menu selections, form load, keyboard input Web applications HTTP requests, form submissions, authentication actions Mobile applications Touches, notifications, sensor changes, location updates Games Key presses, collisions, timer events, score changes IoT systems Sensor readings, threshold changes, device messages Distributed systems Order created, payment completed, message received
Event-driven programs suit interactive systems because they respond to actions when those actions occur. The approach separates the component that announces an event from the components that react to it, which makes software easier to extend because new handlers are added for new responses. The approach also creates challenges. The order of events is harder to follow than a short sequential program. A large program contains many handlers, and slow work inside a handler makes a user interface appear frozen. Clear naming, clear structure, testing and asynchronous techniques manage these problems. REAL-WORLD EXAMPLEs 1.ATM An ATM waits for actions from the customer. Inserting a card, entering a PIN, selecting Withdraw and pressing Cancel are separate events, each with a different handler. The program does not execute every possible action in one fixed sequence because the next action depends on the customer.
2. Student Portal When a student clicks Submit Assignment, the button or web request is the event source. A submission event occurs, the system checks the file and records the submission, then the interface displays confirmation. Other services can respond by sending an email or recording an audit entry. 3. Ticket Purchase A successful payment acts as an event for several parts of a ticketing system. The ticket component generates a ticket, the email component sends confirmation and the reporting component records the sale.
private void btnSave_Click( object sender, EventArgs e) { string name = txtName.Text; lblStatus.Text = "Saved: " + name; } The event source is btnSave. The event is Click. btnSave_Click is the event handler. The first statement reads text from txtName, and the second updates lblStatus. Reading event-driven code starts with identifying the source, the event, the handler and the visible or logical response.