In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, event-driven programming is a
programming paradigm
A programming paradigm is a relatively high-level way to conceptualize and structure the implementation of a computer program. A programming language can be classified as supporting one or more paradigms.
Paradigms are separated along and descri ...
in which the
flow of the program is determined by external
events
Event may refer to:
Gatherings of people
* Ceremony, an event of ritual significance, performed on a special occasion
* Convention (meeting), a gathering of individuals engaged in some common interest
* Event management, the organization of eve ...
.
UI events from
mice
A mouse (: mice) is a small rodent. Characteristically, mice are known to have a pointed snout, small rounded ears, a body-length scaly tail, and a high breeding rate. The best known mouse species is the common house mouse (''Mus musculus' ...
,
keyboards,
touchpads and
touchscreens
A touchscreen (or touch screen) is a type of electronic visual display, display that can detect touch input from a user. It consists of both an input device (a touch panel) and an output device (a visual display). The touch panel is typically l ...
, and external
sensor
A sensor is often defined as a device that receives and responds to a signal or stimulus. The stimulus is the quantity, property, or condition that is sensed and converted into electrical signal.
In the broadest definition, a sensor is a devi ...
inputs are common cases. Events may also be programmatically generated, such as from
messages from other programs, notifications from other
threads, or other
network
Network, networking and networked may refer to:
Science and technology
* Network theory, the study of graphs as a representation of relations between discrete objects
* Network science, an academic field that studies complex networks
Mathematics
...
events.
Event-driven programming is the dominant paradigm used in
graphical user interface
A graphical user interface, or GUI, is a form of user interface that allows user (computing), users to human–computer interaction, interact with electronic devices through Graphics, graphical icon (computing), icons and visual indicators such ...
s applications and network servers.
In an event-driven application, there is generally an
event loop
In computer science, the event loop (also known as message dispatcher, message loop, message pump, or run loop) is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by m ...
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
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
, although the task is easier in languages that provide
high-level abstractions.
Although they do not exactly fit the event-driven model,
interrupt handling
In digital computers, an interrupt (sometimes referred to as a trap) is a request for the processor to ''interrupt'' currently executing code (when permitted), so that the event can be processed in a timely manner. If the request is accepted ...
and
exception handling
In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
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 queue / message driven services (e.g.
AWS SQS) are coupled with their consumers.
Event loop
Because the
event loop
In computer science, the event loop (also known as message dispatcher, message loop, message pump, or run loop) is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by m ...
of retrieving/dispatching of events are common amongst applications, many programming frameworks take care of their implementation and expect the user to provide only the code for the event handlers.
RPG
RPG may refer to:
Military
* Rocket-propelled grenade, a shoulder-launched anti-tank weapon
**''Ruchnoi Protivotankoviy Granatomyot'' (Russian: ''Ручной Противотанковый Гранатомёт''), hand-held anti-tank grenade laun ...
, an early programming language from
IBM
International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
, 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 to 'indicators' (
flags
A flag is a piece of fabric (most often rectangular) with distinctive colours and design. It is used as a symbol, a signalling device, or for decoration. The term ''flag'' is also used to refer to the graphic design employed, and flags have ...
) 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 left-button mouse-click on a command button in a
GUI program may trigger a routine that will open another window, save data to a
database
In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and a ...
or exit the application. Many
IDEs provide the programmer with GUI event templates, allowing the programmer to focus on writing the event code.
Keeping track of history is normally trivial in a sequential program. Because event handlers execute in response to external events, correctly structuring the handlers to work when called in any order can require special attention and planning in an event-driven program.
In addition to writing the event handlers, event handlers also need to be bound to events so that the correct function is called when the event takes place. For UI events, many IDEs combine the two steps: double-click on a button, and the editor creates an (empty) event handler associated with the user clicking the button and opens a text window so you can edit the event handler.
Common uses
Most existing GUI architectures use event-driven programming.
Windows has an
event loop
In computer science, the event loop (also known as message dispatcher, message loop, message pump, or run loop) is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by m ...
. The Java AWT framework processes all UI changes on a single thread, called the
Event dispatching thread The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue. It is an example of the generic concept of event-driven programming, that is popu ...
. Similarly, all UI updates in the Java framework
JavaFX
JavaFX is a software platform for creating and delivering desktop applications, as well as rich web applications that can run across a wide variety of devices. JavaFX has support for desktop computers and web browsers on Microsoft Windows, Linu ...
occur on the JavaFX Application Thread.
Most network servers and frameworks such as Node.js are also event-driven.
Event-Driven Programming in Node.js
Interrupt and exception handling
See also
* Autonomous peripheral operation
In computing, autonomous peripheral operation is a hardware feature found in some microcontroller architectures to off-load certain tasks into embedded autonomous peripherals in order to minimize latencies and improve throughput in hard real-ti ...
* Dataflow programming
In computer programming, dataflow programming is a programming paradigm that models a program as a directed graph of the data flowing between operations, thus implementing dataflow principles and architecture. Dataflow programming languages share ...
* DOM events
DOM (Document Object Model) Events are a signal that something has occurred, or is occurring, and can be triggered by user interactions or by the browser. Client-side scripting languages like JavaScript, JScript, VBScript, and Java can register v ...
* Event-driven architecture
Event-driven architecture (EDA) is a software architecture paradigm concerning the production and detection of Event (computing), events. Event-driven architectures are Continuous design, evolutionary in nature and provide a high degree of fault to ...
* Event stream processing
In computer science, stream processing (also known as event stream processing, data stream processing, or distributed stream processing) is a programming paradigm which views streams, or sequences of events in time, as the central input and output ...
(a similar concept)
* Hardware description language
In computer engineering, a hardware description language (HDL) is a specialized computer language used to describe the structure and behavior of electronic circuits, usually to design application-specific integrated circuits (ASICs) and to progra ...
* Interrupt
In digital computers, an interrupt (sometimes referred to as a trap) is a request for the processor to ''interrupt'' currently executing code (when permitted), so that the event can be processed in a timely manner. If the request is accepted ...
* Inversion of control
* Message-oriented middleware
Message-oriented middleware (MOM) is software or hardware infrastructure supporting sending and receiving messages between distributed systems. Message-oriented middleware is in contrast to streaming-oriented middleware where data is communicate ...
* Programming paradigm
A programming paradigm is a relatively high-level way to conceptualize and structure the implementation of a computer program. A programming language can be classified as supporting one or more paradigms.
Paradigms are separated along and descri ...
* Publish–subscribe pattern
In software architecture, the publish–subscribe pattern (pub/sub) is a messaging pattern in which message senders, called publishers, categorize messages into classes (or ''topics''), and send them without needing to know which components ...
* Reactor pattern
The reactor software design pattern is an event handling strategy that can respond to many potential service requests concurrently. The pattern's key component is an event loop, running in a ''single'' thread or process, which demultiplexes inc ...
* Signal programming
SIGNAL is a programming language based on synchronized dataflow (flows + synchronization): a process is a set of equations on elementary flows describing both data and control.
The SIGNAL formal model provides the capability to describe systems ...
(a similar concept)
* Staged event-driven architecture
''Staged'' is a British television comedy series. Set and filmed during the COVID-19 pandemic in the United Kingdom, primarily using video-conferencing technology, the series stars actors Michael Sheen and David Tennant as fictionalised version ...
(SEDA)
* Time-triggered system (an alternative architecture for computer systems)
* Virtual synchrony
Reliable multicast is any computer networking protocol that provides a '' reliable'' sequence of packets to multiple recipients simultaneously, making it suitable for applications such as multi-receiver file transfer.
Overview
Multicast is a ne ...
, a distributed execution model for event-driven programming
References
External links
Concurrency patterns presentation
given a
scaleconf
Event-Driven Programming: Introduction, Tutorial, History
tutorial by Stephen Ferg
tutorial by Alan Gauld
article by Martin Fowler
article by Jonathan Simon
, article by Chris McDonald
Event Driven Programming using Template Specialization
article by Christopher Diggins
*
Event-Driven Programming and Agents
chapter
LabWindows/CVI Resources
Distributed Publish/Subscribe Event System
an open-source example which is in production on MSN.com and Microsoft.com
{{DEFAULTSORT:Event-Driven Programming
Programming paradigms
Events (computing)
Articles with example pseudocode