Incomputer programming,event-driven programming is aprogramming paradigm in which theflow of the program is determined by externalevents.User interface (UI) events fromkeyboards andmice,touchpads andtouchscreens, and externalsensor inputs are common cases. Events may also be programmatically generated, such as frommessages from other programs, notifications from otherthreads, or othernetwork events.
Event-driven programming is the dominant paradigm used ingraphical user interface (GUI) applications and network servers.
In an event-driven application, there is generally anevent loop that listens for events and then triggers acallback function when one of those events is detected.
Event-driven programs can be written in anyprogramming language, although the task is easier in languages that providehigh-level abstractions.
Although they do not exactly fit the event-driven model,interrupt handling andexception handling have many similarities.
It is important to differentiate betweenevent-driven andmessage-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.[1]
Because theevent 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 fromIBM, whose 1960s design concept was similar to event-driven programming discussed above, provided a built-in mainI/O loop (known as the "program cycle") where the calculations responded in accordance with "indicators" (flags) that were set earlier in the cycle.
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 aGUI program might trigger a routine to save data to adatabase. An "Exit" button might trigger a routine to exit the program. The event loop receives events from all such command buttons and otherGUI 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. ManyIDEs 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.
Most existing GUI architectures use event-driven programming.[2] Windows has themessage loop. The Java AWT framework processes all UI changes on a single thread, called theEvent dispatching thread. Similarly, all UI updates in the Java frameworkJavaFX occur on the JavaFX Application Thread.[3]
Most network servers and frameworks such as Node.js are also event-driven.[4]
This section is empty. You can help byadding to it.(May 2024) |
The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread.