The transition is from a program that follows one fixed path from start to finish, to a program that stays alive and reacts whenever the user, OS, timer, network, or another program sends it an event. An event loop is the mechanism that repeatedly waits for events, takes the next event from a queue, and runs the corresponding handler.innoq+1
1. Single-tasking model
In early systems such as MS-DOS, a program typically ran alone and followed a predictable sequence:
Start ↓ Read input ↓ Process data ↓ Show output ↓ ExitFor example, a DOS program might ask for a filename, read it, print a report, and end. While it was waiting for the user to type something, the program—and often the whole system—was essentially waiting for that one task to finish. MS-DOS was commonly single-user and single-tasking, so programs generally did not need to coordinate with many other running applications.
This model is easy to understand because control flow is linear:
printf("Enter name: "); scanf("%s", name); printf("Hello, %s\n", name);The program decides what happens next.
2. Why the model changed
Modern Windows applications need to handle many things at once:
-
Mouse movement and clicks
-
Keyboard input
-
Window resizing and redraws
-
Menus and buttons
-
Timers
-
File operations
-
Network activity
-
Multiple applications running simultaneously
If a GUI program used the old linear model, it might freeze while waiting for a file to load or a network response. The user could not click another button, resize the window, or type while the program was blocked. Event-driven systems solve this by letting the OS notify the application when something happens, rather than making the application continuously wait.innoq+1
3. What is an event?
An event is a signal that something has happened.
Examples:
| Event | Possible response |
|---|---|
| Mouse click | Open a dialog or submit a form |
| Key press | Update a text field |
| Window resize | Recalculate layout |
| Timer expires | Refresh a clock or dashboard |
| File read completes | Display file contents |
| Network response arrives | Update the UI |
The order in which events occur determines what the program does next. In event-driven programming, execution depends on the sequence of events rather than only the order in which the source code was written.
4. What is an event loop?
An event loop is a continuous cycle:
while (application is running) { event = wait for next event; handler = find handler for event; run handler; }It works like a receptionist:
-
Wait for someone to arrive.
-
Take their request.
-
Send the request to the correct department.
-
Return to waiting for the next person.
In technical terms, the runtime or OS places events in a queue. The event loop takes one event at a time, dispatches it to the registered handler, lets that handler finish, and then processes the next event.innoq+1
A simplified GUI event loop:
Windows OS detects button click ↓ Event is placed in the application’s event queue ↓ Event loop removes the event ↓ Button-click handler runs ↓ UI updates ↓ Event loop waits for the next event5. Example: old flow vs event flow
Linear/single-tasking style
The program controls the sequence.
Event-driven style
Start application Create window and buttons Register handlers: - Button A → openFile() - Button B → saveFile() - Close window → exitApplication() Start event loopThe program does not know in advance whether the user will click Button A, Button B, resize the window, or close it. It simply registers handlers and waits.
Pseudocode:
onButtonClickSave(): save file onButtonClickOpen(): open file dialog read selected file onWindowClose(): stop event loopThe user’s action determines which handler runs.
6. Blocking versus non-blocking behavior
In the old model, a slow operation could block the whole program:
In an event-driven model, the program starts the operation, returns control to the event loop, and handles the completion later:
Start file read ↓ return to event loop User can still click, type, resize window ↓ File-read completion event arrives ↓ Completion handler updates the UIThis is why event loops are central to responsive GUIs, browsers, and I/O-heavy servers. The program avoids actively waiting; it responds when the OS or runtime signals that work is ready.
7. Event loop vs multitasking
An event loop is not the same thing as multitasking, but they work together.
| Concept | Meaning |
|---|---|
| Single-tasking | One program/task runs at a time |
| Multitasking | OS switches rapidly among multiple programs/processes |
| Event-driven programming | A program’s flow is determined by events |
| Event loop | The loop that receives, queues, and dispatches events to handlers |
| Threads | Independent execution paths within a process; can run in parallel on multiple CPU cores |
Modern Windows uses multitasking at the OS level: many applications can run at once. Inside a GUI application, the event loop keeps that application responsive by processing user-interface events. If a handler takes too long, the event loop cannot process the next event, so the application appears frozen.innoq+1
8. Mental model shift
Instead of asking, “What line runs next?”, you ask:
-
What events can occur?
-
Which handler should respond to each event?
-
Can this handler finish quickly?
-
Should long work be moved to another thread or asynchronous task?
That is the core transition: from a program that controls time to a program that responds to time. Event loops let one thread remain useful by handling events as they become ready instead of blocking on each slow operation
0