Event-driven programming: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
External links: Removed dead link.
 
imported>WakingLili
Rewrote for clarity
 
Line 14: Line 14:


==Event loop==
==Event loop==
{{Main article|Event loop}}Because the [[main loop|event loop]] 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.   
{{Main article|Event loop}}Because the [[main loop|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.   


[[IBM RPG|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 to 'indicators' ([[flag (computing)|flags]]) that were set earlier in the cycle.  
[[IBM RPG|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" ([[flag (computing)|flags]]) that were set earlier in the cycle.  


===Event handlers===
===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 [[graphical user interface|GUI]] program may trigger a routine that will open another window, save data to a [[database]] or exit the application. Many [[Integrated development environment|IDEs]] provide the programmer with GUI event templates, allowing the programmer to focus on writing the event code.
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 [[graphical user interface|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 [[Graphical widget|GUI elements]], dispatching the appropriate event handler routine for each button.


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.
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 [[Integrated development environment|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 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.
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==
==Common uses==
Line 65: Line 65:
==External links==
==External links==
*[http://shairosenfeld.com/concurrency.html Concurrency patterns presentation] given at [http://scaleconf.org scaleconf]
*[http://shairosenfeld.com/concurrency.html Concurrency patterns presentation] given at [http://scaleconf.org scaleconf]
*[http://eventdrivenpgm.sourceforge.net/ Event-Driven Programming: Introduction, Tutorial, History], tutorial by Stephen Ferg
*[https://eventdrivenpgm.sourceforge.net/ Event-Driven Programming: Introduction, Tutorial, History], tutorial by Stephen Ferg
*[http://www.alan-g.me.uk/l2p/tutevent.htm Event-Driven Programming], tutorial by Alan Gauld
*[http://www.alan-g.me.uk/l2p/tutevent.htm Event-Driven Programming], tutorial by Alan Gauld
*[http://www.martinfowler.com/eaaDev/EventCollaboration.html Event Collaboration], article by Martin Fowler
*[http://www.martinfowler.com/eaaDev/EventCollaboration.html Event Collaboration], article by Martin Fowler

Latest revision as of 16:49, 9 October 2025

Template:Short description

In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by external events. UI events from mice, keyboards, 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 interfaces 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 queue / message driven services (e.g. AWS SQS) are coupled with their consumers.[1]

Event loop

Template:Main articleBecause 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.[2] Windows has an event 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.[3]

Most network servers and frameworks such as Node.js are also event-driven.[4]

Interrupt and exception handling

Template:Empty section

See also

References

Template:Reflist

External links

Template:Programming paradigms navbox Template:Types of programming languages

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. Event-Driven Programming in Node.js.