ArticleslgStudy

computer science

Event-driven programming

Event-driven programming is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Event-driven programming rather than just read about it. In short: In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by external events. User interface (UI) events from keyboards and mice, touchpads and touchscreens, and external sensor inputs are common cases.

Key takeaways

  • Event-driven programming belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Event-driven programming to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Event-driven programming from memory before moving on to harder problems.

Reference excerpt

In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by external events. User interface (UI) events from keyboards and mice, touchpads and touchscreens, and external sensor inputs are common cases. Events may also be programmatically generated, such as from messages from other programs, notifications from other threads, or other network events. Event-driven programming is the dominant paradigm used in graphical user interface (GUI) applications and network servers. In an event-driven application, there is generally an event loop that listens for events and then triggers a callback function when one of those events is detected. Event-driven programs can be written in any programming language, although the task is easier in languages that provide high-level abstractions. Although they do not exactly fit the event-driven model, interrupt handling and exception handling have many similarities. It is important to differentiate between event-driven and message-driven (aka queue-driven) paradigms: event-driven services (e.g. AWS SNS) are decoupled from their consumers, whereas message/queue-driven services (e.g. AWS SQS) are coupled with their consumers.

Event loop Because the event loop that retrieves and dispatches events is common amongst applications, many programming frameworks provide an implementation of an event loop, and the application developer only needs to write the event handlers. RPG, an early programming language from IBM, whose 1960s design concept was similar to event-driven programming discussed above, provided a built-in main I/O loop (known as the "program cycle") where the calculations responded in accordance with "indicators" (flags) that were set earlier in the cycle.

Event handlers The actual logic is contained in event handler routines. These routines handle the events to which the main program will respond. For example, a single mouse-click on a "Save" command button in a GUI program might trigger a routine to save data to a database. An "Exit" button might trigger a routine to exit the program. The event loop receives events from all such command buttons and other GUI elements, dispatching the appropriate event handler routine for each button. Event handler routines need to be bound to specific events, so the event loop can dispatch the correct routine in response to the event. Many IDEs simplify this process by providing the programmer with an event handling template for each specific event (such as a button click), allowing the programmer to focus on writing the event-handling code. In a sequential program, keeping track of execution order and history is normally trivial. But in an event-driven program, event handlers execute non-sequentially in response to external events. Special attention and planning is required to correctly structure the event handlers to work when called in any order.

Common uses Most existing GUI architectures use event-driven programming. Windows has the message loop. The Java AWT framework processes all UI changes on a single thread, called the Event dispatching thread. Similarly, all UI updates in the Java framework JavaFX occur on the JavaFX Application Thread. Most network servers and frameworks such as Node.js are also event-driven.

Interrupt and exception handling

See also Autonomous peripheral operation Dataflow programming DOM events Event-driven architecture Event stream processing (a similar concept) Hardware description language Interrupt Inversion of control Message-oriented middleware Programming paradigm Publish–subscribe pattern Reactor pattern Signals and slots – Language construct in Qt framework Staged event-driven architecture (SEDA) Time-triggered system (an alternative architecture for computer systems) Virtual synchrony, a distributed execution model for event-driven programming

References

External links Concurrency patterns presentation given at scaleconf Event-Driven Programming: Introduction, Tutorial, History, tutorial by Stephen Ferg Event-Driven Programming, tutorial by Alan Gauld Event Collaboration, article by Martin Fowler Rethinking Swing Threading, article by Jonathan Simon The event-driven programming style Archived 2005-02-20 at the Wayback Machine, article by Chris McDonald Event Driven Programming using Template Specialization, article by Christopher Diggins Schiffer, S.; Frohlich, J.H. (1994). "Concepts and architecture of Vista-a multiparadigm programming environment". Proceedings of 1994 IEEE Symposium on Visual Languages. pp. 40–47. doi:10.1109/VL.1994.363642. ISBN 0-8186-6660-9. S2CID 15927869. Event-Driven Programming and Agents, chapter LabWindows/CVI Resources[link removed] Distributed Publish/Subscribe Event System, an open-source example which is in production on MSN.com and Microsoft.com

Worked examples

Example 1 — a first encounter with Event-driven programming

Start with the simplest possible case. Write down what Event-driven programming claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Event-driven programming before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Event-driven programming ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Event-driven programming

In research
Event-driven programming appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Event-driven programming in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Event-driven programming is common in secondary-school and first-year university syllabi. It links to neighbouring topics Events (computing), Programming paradigms, so understanding it makes those chapters shorter.
In everyday life
Look for Event-driven programming outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Event-driven programming” →

How to study Event-driven programming in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Event-driven programming means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Event-driven programming out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Event-driven programming in simple terms?

In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by external events. User interface (UI) events from keyboards and mice, touchpads and touchscreens, and external sensor inputs are common cases.

Why does Event-driven programming matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Event-driven programming?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Event-driven programming.

Tags

  • Events (computing)
  • Programming paradigms

Keep exploring